Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds new trait-method-added lint #891

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Comment on lines +22 to +30
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This portion is correctly implemented, but would be lovely if we had a test case for one more situation that users might find confusing.

Users might think that marking the new method #[doc(hidden)] may resolve this lint. It shouldn't, and it'd be great to have a test for it.

For bonus points, if you're interested, you could take a look at our other "trait item added without default" lints and make sure they all have a "#[doc(hidden)] on the new item" test case as well. If not, more PRs are welcome!

}
}
}
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 @@ -839,6 +839,7 @@ add_lints!(
trait_associated_type_added,
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
Loading