Skip to content

Commit

Permalink
adds new trait-method-added lint (#891)
Browse files Browse the repository at this point in the history
* adds new trait-method-added lint

* corrects lint to account for traits that have also become sealed, and adds tests for it
  • Loading branch information
PedroTurik authored Aug 29, 2024
1 parent 2466f7c commit 9900692
Show file tree
Hide file tree
Showing 8 changed files with 527 additions and 0 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}}"),
)
1 change: 1 addition & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,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
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 9900692

Please sign in to comment.