Skip to content

Commit

Permalink
New lint: Trait #[must_use] added (#280)
Browse files Browse the repository at this point in the history
* New lint: trait_must_use_added

* query.rs update

* Self-reviewed some comments.

Co-authored-by: Bartosz Smolarczyk <[email protected]>
  • Loading branch information
SmolSir and Bartosz Smolarczyk authored Jan 11, 2023
1 parent 025cac8 commit bb0c63e
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/lints/trait_must_use_added.ron
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}}"),
)
1 change: 1 addition & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ add_lints!(
struct_repr_c_removed,
struct_repr_transparent_removed,
trait_missing,
trait_must_use_added,
trait_unsafe_added,
trait_unsafe_removed,
tuple_struct_to_plain_struct,
Expand Down
6 changes: 6 additions & 0 deletions test_crates/trait_must_use_added/new/Cargo.toml
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]
44 changes: 44 additions & 0 deletions test_crates/trait_must_use_added/new/src/lib.rs
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 {}
6 changes: 6 additions & 0 deletions test_crates/trait_must_use_added/old/Cargo.toml
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]
35 changes: 35 additions & 0 deletions test_crates/trait_must_use_added/old/src/lib.rs
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 {}
26 changes: 26 additions & 0 deletions test_outputs/trait_must_use_added.output.ron
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"),
},
],
}

0 comments on commit bb0c63e

Please sign in to comment.