Skip to content

Commit

Permalink
Merge branch 'main' into update_path_dependency_lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
obi1kenobi authored Sep 2, 2024
2 parents d1049b6 + cf060a6 commit b6fb4bd
Show file tree
Hide file tree
Showing 10 changed files with 541 additions and 11 deletions.
60 changes: 60 additions & 0 deletions src/lints/trait_method_added.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
SemverQuery(
id: "trait_method_added",
human_readable_name: "pub trait method added",
description: "A non-sealed public trait added a new method without a default implementation",
required_update: Major,
lint_level: Deny,
reference_link: Some("https://doc.rust-lang.org/cargo/reference/semver.html#trait-new-item-no-default"),
query: r#"
{
CrateDiff {
current {
item {
... on Trait {
visibility_limit @filter(op: "=", value: ["$public"]) @output
name @output
importable_path {
path @output @tag
public_api @filter(op: "=", value: ["$true"])
}
method {
method_name: name @output @tag
has_body @filter(op: "!=", value: ["$true"])
span_: span @optional {
filename @output
begin_line @output
}
}
}
}
}
baseline {
item {
... on Trait {
visibility_limit @filter(op: "=", value: ["$public"])
sealed @filter(op: "!=", value: ["$true"])
importable_path {
path @filter(op: "=", value: ["%path"])
public_api @filter(op: "=", value: ["$true"])
}
method @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) {
name @filter(op: "=", value: ["%method_name"])
}
}
}
}
}
}"#,
arguments: {
"true": true,
"public": "public",
"zero": 0,
},
error_message: "A non-sealed public trait added a new method without a default implementation, which breaks downstream implementations of the trait",
per_result_error_template: Some("trait method {{join \"::\" path}}::{{method_name}} in file {{span_filename}}:{{span_begin_line}}"),
)
14 changes: 5 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ fn main() {

configure_color(args.color_choice);
let mut config = GlobalConfig::new();
config.set_log_level(args.verbosity.log_level());

if args.bugreport {
print_issue_url(&mut config);
std::process::exit(0);
} else if args.list {
exit_on_error(true, || {
let mut config = GlobalConfig::new();
config.set_log_level(args.check_release.verbosity.log_level());

let queries = SemverQuery::all_queries();
let mut rows = vec![["id", "type", "description"], ["==", "====", "==========="]];
for query in queries.values() {
Expand Down Expand Up @@ -90,8 +88,6 @@ fn main() {
None => args.check_release,
};

config.set_log_level(check_release.verbosity.log_level());

let check: cargo_semver_checks::Check = check_release.into();

let report = exit_on_error(config.is_error(), || check.check_release(&mut config));
Expand Down Expand Up @@ -241,6 +237,10 @@ struct SemverChecks {
/// Choose whether to output colors
#[arg(long = "color", global = true, value_name = "WHEN", value_enum)]
color_choice: Option<clap::ColorChoice>,

// docstring for help is on the `clap_verbosity_flag::Verbosity` struct itself
#[command(flatten)]
verbosity: clap_verbosity_flag::Verbosity<clap_verbosity_flag::InfoLevel>,
}

/// Check your crate for semver violations.
Expand Down Expand Up @@ -390,10 +390,6 @@ struct CheckRelease {
/// `x86_64-unknown-linux-gnu`.
#[arg(long = "target")]
build_target: Option<String>,

// docstring for help is on the `clap_verbosity_flag::Verbosity` struct itself
#[command(flatten)]
verbosity: clap_verbosity_flag::Verbosity<clap_verbosity_flag::InfoLevel>,
}

impl From<CheckRelease> for cargo_semver_checks::Check {
Expand Down
10 changes: 9 additions & 1 deletion src/query.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{collections::BTreeMap, sync::Arc};

use ron::extensions::Extensions;
use serde::{Deserialize, Serialize};
use trustfall::TransparentValue;

Expand Down Expand Up @@ -128,7 +129,13 @@ impl SemverQuery {
pub fn all_queries() -> BTreeMap<String, SemverQuery> {
let mut queries = BTreeMap::default();
for (id, query_text) in get_queries() {
let query: SemverQuery = ron::from_str(query_text).unwrap_or_else(|e| {
let mut deserializer = ron::Deserializer::from_str_with_options(
query_text,
ron::Options::default().with_default_extension(Extensions::IMPLICIT_SOME),
)
.expect("Failed to construct deserializer.");

let query = Self::deserialize(&mut deserializer).unwrap_or_else(|e| {
panic!(
"\
Failed to parse a query: {e}
Expand Down Expand Up @@ -840,6 +847,7 @@ add_lints!(
trait_associated_type_default_removed,
trait_associated_type_now_doc_hidden,
trait_default_impl_removed,
trait_method_added,
trait_method_missing,
trait_method_now_doc_hidden,
trait_method_unsafe_added,
Expand Down
2 changes: 1 addition & 1 deletion src/snapshot_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fn assert_integration_test(test_name: &str, invocation: &[&str]) {
config.set_stdout(Box::new(stdout.clone()));
config.set_stderr(Box::new(stderr.clone()));
config.set_color_choice(false);
config.set_log_level(arguments.check_release.verbosity.log_level());
config.set_log_level(arguments.verbosity.log_level());

let check = Check::from(arguments.check_release);

Expand Down
7 changes: 7 additions & 0 deletions test_crates/trait_method_added/new/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "trait_method_added"
version = "0.1.0"
edition = "2021"

[dependencies]
49 changes: 49 additions & 0 deletions test_crates/trait_method_added/new/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
mod sealed {
pub(crate) trait Sealed {}
}

// ---- Should be reported ----
pub trait WillGainMethodWithoutDefault {
fn one_method(self);
}

pub trait WillGainAnotherMethodWithoutDefault {
fn one_method(self);
fn two_method(self);
}

pub trait WillGainMultipleMethodsWithoutDefault {
fn one_method(self);
fn two_method(self);
}

pub trait WillGainMethodWithoutDefaultAndSeal: sealed::Sealed {
fn one_method(self);
}

pub trait WIllGainDocHiddenMethodWithoutDefault {
#[doc(hidden)]
fn one_method(self);
}

// ---- Should not be reported ----
pub trait WillGainMethodWithDefault {
fn one_method(self) {}
}

pub trait WillGainAnotherMethodWithDefault {
fn one_method(self);
fn two_method(self) {}
}

pub trait WillGainMethodWithoutDefaultSealed: sealed::Sealed {
fn one_method(self);
}

pub trait WillGainMethodWithoutDefaultAndLoseSeal {
fn one_method(self);
}

pub trait WillKeepAMethodWithoutDefault {
fn one_method(self);
}
7 changes: 7 additions & 0 deletions test_crates/trait_method_added/old/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "trait_method_added"
version = "0.1.0"
edition = "2021"

[dependencies]
31 changes: 31 additions & 0 deletions test_crates/trait_method_added/old/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
mod sealed {
pub(crate) trait Sealed {}
}

// ---- Should be reported ----
pub trait WillGainMethodWithoutDefault {}

pub trait WillGainAnotherMethodWithoutDefault {
fn one_method(self);
}

pub trait WillGainMultipleMethodsWithoutDefault {}

pub trait WillGainMethodWithoutDefaultAndSeal {}

pub trait WIllGainDocHiddenMethodWithoutDefault {}

// ---- Should not be reported ----
pub trait WillGainMethodWithDefault {}

pub trait WillGainAnotherMethodWithDefault {
fn one_method(self);
}

pub trait WillGainMethodWithoutDefaultSealed: sealed::Sealed {}

pub trait WillGainMethodWithoutDefaultAndLoseSeal: sealed::Sealed {}

pub trait WillKeepAMethodWithoutDefault {
fn one_method(self);
}
Loading

0 comments on commit b6fb4bd

Please sign in to comment.