-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New lint: Trait
#[must_use]
added (#280)
* New lint: trait_must_use_added * query.rs update * Self-reviewed some comments. Co-authored-by: Bartosz Smolarczyk <[email protected]>
- Loading branch information
Showing
7 changed files
with
182 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,64 @@ | ||
SemverQuery( | ||
id: "trait_must_use_added", | ||
human_readable_name: "trait #[must_use] added", | ||
description: "A trait 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 { | ||
baseline { | ||
item { | ||
... on Trait { | ||
visibility_limit @filter(op: "=", value: ["$public"]) @output | ||
name @tag @output | ||
importable_path { | ||
path @tag @output | ||
} | ||
attribute @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) { | ||
content { | ||
base @filter(op: "=", value: ["$must_use"]) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
current { | ||
item { | ||
... on Trait { | ||
visibility_limit @filter(op: "=", value: ["$public"]) | ||
name @filter(op: "=", value: ["%name"]) | ||
importable_path { | ||
path @filter(op: "=", value: ["%path"]) | ||
} | ||
attribute { | ||
new_attr: raw_attribute @output | ||
content { | ||
base @filter(op: "=", value: ["$must_use"]) | ||
} | ||
} | ||
span_: span @optional { | ||
filename @output | ||
begin_line @output | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}"#, | ||
arguments: { | ||
"public": "public", | ||
"must_use": "must_use", | ||
"zero": 0, | ||
}, | ||
error_message: "A trait is now #[must_use]. Downstream crates that called a function returning an impl trait or dyn trait of this trait will get a compiler lint.", | ||
per_result_error_template: Some("trait {{join \"::\" path}} 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,6 @@ | ||
[package] | ||
name = "trait_must_use_added" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
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,44 @@ | ||
// These traits did not have the #[must_use] attribute in the old version. | ||
// Addition of the attribute should be reported by this rule. | ||
|
||
#[must_use] | ||
pub trait TraitToMustUseTrait {} | ||
|
||
#[must_use = "Foo"] | ||
pub trait TraitToMustUseMessageTrait {} | ||
|
||
|
||
// These traits had the #[must_use] attribute in the old version. Changes of | ||
// the attribute, including deletion, should NOT be reported by this rule. | ||
|
||
pub trait MustUseTraitToTrait {} | ||
|
||
#[must_use = "Foo"] | ||
pub trait MustUseTraitToMustUseMessageTrait {} | ||
|
||
|
||
// These traits 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 by this rule. | ||
|
||
pub trait MustUseMessageTraitToTrait {} | ||
|
||
#[must_use] | ||
pub trait MustUseMessageTraitToMustUseTrait {} | ||
|
||
#[must_use = "Baz"] | ||
pub trait MustUseMessageTraitToMustUseMessageTrait {} | ||
|
||
|
||
// This trait is private and should NOT be reported by this rule. | ||
|
||
#[must_use] | ||
trait MustUsePrivateTrait {} | ||
|
||
|
||
// This trait was added in the new version of the crate with its 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. | ||
|
||
#[must_use] | ||
pub trait MustUseNewTrait {} |
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,6 @@ | ||
[package] | ||
name = "trait_must_use_added" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
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,35 @@ | ||
// These traits did not have the #[must_use] attribute in the old version. | ||
// Addition of the attribute should be reported by this rule. | ||
|
||
pub trait TraitToMustUseTrait {} | ||
|
||
pub trait TraitToMustUseMessageTrait {} | ||
|
||
|
||
// These traits had the #[must_use] attribute in the old version. Changes of | ||
// the attribute, including deletion, should NOT be reported by this rule. | ||
|
||
#[must_use] | ||
pub trait MustUseTraitToTrait {} | ||
|
||
#[must_use] | ||
pub trait MustUseTraitToMustUseMessageTrait {} | ||
|
||
|
||
// These traits 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 by this rule. | ||
|
||
#[must_use = "Foo"] | ||
pub trait MustUseMessageTraitToTrait {} | ||
|
||
#[must_use = "Foo"] | ||
pub trait MustUseMessageTraitToMustUseTrait {} | ||
|
||
#[must_use = "Foo"] | ||
pub trait MustUseMessageTraitToMustUseMessageTrait {} | ||
|
||
|
||
// This trait is private and should NOT be reported by this rule. | ||
|
||
trait MustUsePrivateTrait {} |
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,26 @@ | ||
{ | ||
"./test_crates/trait_must_use_added/": [ | ||
{ | ||
"name": String("TraitToMustUseTrait"), | ||
"new_attr": String("#[must_use]"), | ||
"path": List([ | ||
String("trait_must_use_added"), | ||
String("TraitToMustUseTrait"), | ||
]), | ||
"span_begin_line": Uint64(5), | ||
"span_filename": String("src/lib.rs"), | ||
"visibility_limit": String("public"), | ||
}, | ||
{ | ||
"name": String("TraitToMustUseMessageTrait"), | ||
"new_attr": String("#[must_use = \"Foo\"]"), | ||
"path": List([ | ||
String("trait_must_use_added"), | ||
String("TraitToMustUseMessageTrait"), | ||
]), | ||
"span_begin_line": Uint64(8), | ||
"span_filename": String("src/lib.rs"), | ||
"visibility_limit": String("public"), | ||
}, | ||
], | ||
} |