-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New lint: Inherent method
#[must_use]
added (#283)
* New lint: inherent_method_must_use_added * Self-reviewed some comments. * Missing comma in query.rs * Additional tests for enum, union & item type changed. * Update test outputs for enum_missing & struct_missing * Update output for inherent_method_must_use_added * Add `publish = false` to Cargo.toml * Lint & update of output file. * Update src/lints/inherent_method_must_use_added.ron Co-authored-by: Predrag Gruevski <[email protected]> * Apply suggestions from code review Co-authored-by: Bartosz Smolarczyk <[email protected]> Co-authored-by: Predrag Gruevski <[email protected]>
- Loading branch information
1 parent
ad277d1
commit f825136
Showing
17 changed files
with
979 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
SemverQuery( | ||
id: "inherent_method_must_use_added", | ||
human_readable_name: "inherent method #[must_use] added", | ||
description: "An inherent method has been marked with #[must_use].", | ||
required_update: Minor, | ||
|
||
// TODO: Change the reference link to point to the cargo semver reference | ||
// once it has a section on attribute #[must_use]. | ||
reference_link: Some("https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute"), | ||
query: r#" | ||
{ | ||
CrateDiff { | ||
current { | ||
item { | ||
... on ImplOwner { | ||
visibility_limit @filter(op: "=", value: ["$public"]) @output | ||
name @tag @output | ||
owner_type: __typename @tag @output | ||
importable_path { | ||
path @tag @output | ||
} | ||
inherent_impl { | ||
method { | ||
method_visibility: visibility_limit @filter(op: "=", value: ["$public"]) @output | ||
method_name: name @tag @output | ||
attribute { | ||
new_attr: raw_attribute @output | ||
content { | ||
base @filter(op: "=", value: ["$must_use"]) | ||
} | ||
} | ||
span_: span @optional { | ||
filename @output | ||
begin_line @output | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
baseline { | ||
item { | ||
... on ImplOwner { | ||
visibility_limit @filter(op: "=", value: ["$public"]) | ||
name @filter(op: "=", value: ["%name"]) | ||
__typename @filter(op: "=", value: ["%owner_type"]) | ||
importable_path { | ||
path @filter(op: "=", value: ["%path"]) | ||
} | ||
inherent_impl { | ||
method { | ||
visibility_limit @filter(op: "=", value: ["$public"]) | ||
name @filter(op: "=", value: ["%method_name"]) | ||
attribute @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) { | ||
content { | ||
base @filter(op: "=", value: ["$must_use"]) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}"#, | ||
arguments: { | ||
"public": "public", | ||
"must_use": "must_use", | ||
"zero": 0, | ||
}, | ||
error_message: "An inherent method is now #[must_use]. Downstream crates that did not use its return value will get a compiler lint.", | ||
per_result_error_template: Some("method {{join \"::\" path}}::{{method_name}} in {{span_filename}}:{{span_begin_line}}"), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
publish = false | ||
name = "inherent_method_must_use_added" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
82 changes: 82 additions & 0 deletions
82
test_crates/inherent_method_must_use_added/new/src/enum_inherent_method_must_use_added.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
pub enum EnumWithMustUseMethods { | ||
Bar, | ||
} | ||
|
||
impl EnumWithMustUseMethods { | ||
|
||
// These methods did not have the #[must_use] attribute in the old version. | ||
// Addition of the attribute should be reported. | ||
|
||
#[must_use] | ||
pub fn MethodToMustUseMethod(&self) {} | ||
|
||
#[must_use = "Foo"] | ||
pub fn MethodToMustUseMessageMethod(&self) {} | ||
|
||
|
||
// These methods had the #[must_use] attribute in the old version. Changes of | ||
// the attribute, including deletion, should not be reported. | ||
|
||
pub fn MustUseMethodToMethod(&self) {} | ||
|
||
#[must_use = "Foo"] | ||
pub fn MustUseMethodToMustUseMessageMethod(&self) {} | ||
|
||
|
||
// These methods had the #[must_use] attribute in the old version. | ||
// They also included the user-defined warning message. Changes of | ||
// the attribute, including deletion, should not be reported. | ||
|
||
pub fn MustUseMessageMethodToMethod(&self) {} | ||
|
||
#[must_use] | ||
pub fn MustUseMessageMethodToMustUseMethod(&self) {} | ||
|
||
#[must_use = "Baz"] | ||
pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} | ||
} | ||
|
||
|
||
// This public enum's inherent method did not have the #[must_use] attribute | ||
// in the old version. Because the method is private, adding the attribute | ||
// should NOT be reported. | ||
|
||
pub enum EnumWithPrivateMustUseMethods { | ||
Bar, | ||
} | ||
|
||
impl EnumWithPrivateMustUseMethods { | ||
|
||
#[must_use] | ||
fn PrivateMethodToPrivateMustUseMethod(&self) {} | ||
} | ||
|
||
|
||
// This enum is private and adding #[must_use] to its inherent method | ||
// should NOT be reported. | ||
|
||
enum PrivateEnumWithMustUseMethods { | ||
Bar, | ||
} | ||
|
||
impl PrivateEnumWithMustUseMethods { | ||
|
||
#[must_use] | ||
fn PrivateEnumMethodToPrivateEnumMustUseMethod(&self) {} | ||
} | ||
|
||
|
||
// This enum and its inherent method were added in the new version of the | ||
// crate, together with the method's attribute. | ||
// It should NOT be reported by this rule to avoid duplicate lints. | ||
// It should be reported as a new pub type that is part of the crate's API. | ||
|
||
pub enum NewEnumWithMustUseMethods { | ||
Bar, | ||
} | ||
|
||
impl NewEnumWithMustUseMethods { | ||
|
||
#[must_use] | ||
pub fn NewEnumMustUseMethod(&self) {} | ||
} |
163 changes: 163 additions & 0 deletions
163
...nherent_method_must_use_added/new/src/item_type_changed_inherent_method_must_use_added.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
// This test file pair contains test cases against items changing the type | ||
// between the old and new versions while their names and inherent methods | ||
// remain unchanged. | ||
// This can result in false-positives by reporting an addition of #[must_use] | ||
// to an inherent method while the ImplOwner of the method has changed, making | ||
// the new version method no more related to the old version method. | ||
|
||
pub struct EnumToStructWithMustUseMethods {} | ||
|
||
impl EnumToStructWithMustUseMethods { | ||
|
||
#[must_use] | ||
pub fn MethodToMustUseMethod(&self) {} | ||
|
||
#[must_use = "Foo"] | ||
pub fn MethodToMustUseMessageMethod(&self) {} | ||
|
||
pub fn MustUseMethodToMethod(&self) {} | ||
|
||
#[must_use = "Foo"] | ||
pub fn MustUseMethodToMustUseMessageMethod(&self) {} | ||
|
||
pub fn MustUseMessageMethodToMethod(&self) {} | ||
|
||
#[must_use] | ||
pub fn MustUseMessageMethodToMustUseMethod(&self) {} | ||
|
||
#[must_use = "Baz"] | ||
pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} | ||
} | ||
|
||
|
||
pub union EnumToUnionWithMustUseMethods { | ||
bar: usize, | ||
} | ||
|
||
impl EnumToUnionWithMustUseMethods { | ||
|
||
#[must_use] | ||
pub fn MethodToMustUseMethod(&self) {} | ||
|
||
#[must_use = "Foo"] | ||
pub fn MethodToMustUseMessageMethod(&self) {} | ||
|
||
pub fn MustUseMethodToMethod(&self) {} | ||
|
||
#[must_use = "Foo"] | ||
pub fn MustUseMethodToMustUseMessageMethod(&self) {} | ||
|
||
pub fn MustUseMessageMethodToMethod(&self) {} | ||
|
||
#[must_use] | ||
pub fn MustUseMessageMethodToMustUseMethod(&self) {} | ||
|
||
#[must_use = "Baz"] | ||
pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} | ||
} | ||
|
||
|
||
pub enum StructToEnumWithMustUseMethods { | ||
Bar, | ||
} | ||
|
||
impl StructToEnumWithMustUseMethods { | ||
|
||
#[must_use] | ||
pub fn MethodToMustUseMethod(&self) {} | ||
|
||
#[must_use = "Foo"] | ||
pub fn MethodToMustUseMessageMethod(&self) {} | ||
|
||
pub fn MustUseMethodToMethod(&self) {} | ||
|
||
#[must_use = "Foo"] | ||
pub fn MustUseMethodToMustUseMessageMethod(&self) {} | ||
|
||
pub fn MustUseMessageMethodToMethod(&self) {} | ||
|
||
#[must_use] | ||
pub fn MustUseMessageMethodToMustUseMethod(&self) {} | ||
|
||
#[must_use = "Baz"] | ||
pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} | ||
} | ||
|
||
|
||
pub union StructToUnionWithMustUseMethods { | ||
bar: usize, | ||
} | ||
|
||
impl StructToUnionWithMustUseMethods { | ||
|
||
#[must_use] | ||
pub fn MethodToMustUseMethod(&self) {} | ||
|
||
#[must_use = "Foo"] | ||
pub fn MethodToMustUseMessageMethod(&self) {} | ||
|
||
pub fn MustUseMethodToMethod(&self) {} | ||
|
||
#[must_use = "Foo"] | ||
pub fn MustUseMethodToMustUseMessageMethod(&self) {} | ||
|
||
pub fn MustUseMessageMethodToMethod(&self) {} | ||
|
||
#[must_use] | ||
pub fn MustUseMessageMethodToMustUseMethod(&self) {} | ||
|
||
#[must_use = "Baz"] | ||
pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} | ||
} | ||
|
||
|
||
pub enum UnionToEnumWithMustUseMethods { | ||
Bar, | ||
} | ||
|
||
impl UnionToEnumWithMustUseMethods { | ||
|
||
#[must_use] | ||
pub fn MethodToMustUseMethod(&self) {} | ||
|
||
#[must_use = "Foo"] | ||
pub fn MethodToMustUseMessageMethod(&self) {} | ||
|
||
pub fn MustUseMethodToMethod(&self) {} | ||
|
||
#[must_use = "Foo"] | ||
pub fn MustUseMethodToMustUseMessageMethod(&self) {} | ||
|
||
pub fn MustUseMessageMethodToMethod(&self) {} | ||
|
||
#[must_use] | ||
pub fn MustUseMessageMethodToMustUseMethod(&self) {} | ||
|
||
#[must_use = "Baz"] | ||
pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} | ||
} | ||
|
||
|
||
pub struct UnionToStructWithMustUseMethods {} | ||
|
||
impl UnionToStructWithMustUseMethods { | ||
|
||
#[must_use] | ||
pub fn MethodToMustUseMethod(&self) {} | ||
|
||
#[must_use = "Foo"] | ||
pub fn MethodToMustUseMessageMethod(&self) {} | ||
|
||
pub fn MustUseMethodToMethod(&self) {} | ||
|
||
#[must_use = "Foo"] | ||
pub fn MustUseMethodToMustUseMessageMethod(&self) {} | ||
|
||
pub fn MustUseMessageMethodToMethod(&self) {} | ||
|
||
#[must_use] | ||
pub fn MustUseMessageMethodToMustUseMethod(&self) {} | ||
|
||
#[must_use = "Baz"] | ||
pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// This crate's test cases were separated into smaller files for easier | ||
// management and debugging. | ||
|
||
pub mod enum_inherent_method_must_use_added; | ||
|
||
pub mod struct_inherent_method_must_use_added; | ||
|
||
pub mod union_inherent_method_must_use_added; | ||
|
||
pub mod item_type_changed_inherent_method_must_use_added; |
Oops, something went wrong.