-
-
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.
Adds union_must_use_added lint (#845)
* adds union_must_use_added lint, with the correct changes in the documentation * fixes windows double slashes * Apply suggestions from code review --------- Co-authored-by: Predrag Gruevski <[email protected]>
- Loading branch information
1 parent
58d76d6
commit 941e14c
Showing
8 changed files
with
220 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
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: "union_must_use_added", | ||
human_readable_name: "union #[must_use] added", | ||
description: "A union has been marked with #[must_use].", | ||
required_update: Minor, | ||
lint_level: Deny, | ||
reference_link: Some("https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute"), | ||
query: r#" | ||
{ | ||
CrateDiff { | ||
current { | ||
item { | ||
... on Union { | ||
visibility_limit @filter(op: "=", value: ["$public"]) | ||
name @output | ||
importable_path { | ||
path @tag @output | ||
public_api @filter(op: "=", value: ["$true"]) | ||
} | ||
attribute { | ||
new_attr: raw_attribute @output | ||
content { | ||
base @filter(op: "=", value: ["$must_use"]) | ||
} | ||
} | ||
span_: span @optional { | ||
filename @output | ||
begin_line @output | ||
} | ||
} | ||
} | ||
} | ||
baseline { | ||
item { | ||
... on Union { | ||
visibility_limit @filter(op: "=", value: ["$public"]) @output | ||
importable_path { | ||
path @filter(op: "=", value: ["%path"]) | ||
public_api @filter(op: "=", value: ["$true"]) | ||
} | ||
attribute @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) { | ||
content { | ||
base @filter(op: "=", value: ["$must_use"]) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}"#, | ||
arguments: { | ||
"public": "public", | ||
"must_use": "must_use", | ||
"true": true, | ||
"zero": 0, | ||
}, | ||
error_message: "A union is now #[must_use]. Downstream crates that did not use its value will get a compiler lint.", | ||
per_result_error_template: Some("union {{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 = "union_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,62 @@ | ||
// These unions did not have the #[must_use] attribute in the old version. | ||
// Addition of the attribute should be reported by this rule. | ||
|
||
#[must_use] | ||
pub union UnionToMustUseUnion { | ||
bar: u64, | ||
} | ||
|
||
#[must_use = "Foo"] | ||
pub union UnionToMustUseMessageUnion { | ||
bar: u64, | ||
} | ||
|
||
|
||
// These unions had the #[must_use] attribute in the old version. Changes of | ||
// the attribute, including deletion, should NOT be reported by this rule. | ||
|
||
pub union MustUseUnionToUnion { | ||
bar: u64, | ||
} | ||
|
||
#[must_use = "Foo"] | ||
pub union MustUseUnionToMustUseMessageUnion { | ||
bar: u64, | ||
} | ||
|
||
|
||
// These unions 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 union MustUseMessageUnionToUnion { | ||
bar: u64, | ||
} | ||
|
||
#[must_use] | ||
pub union MustUseMessageUnionToMustUseUnion { | ||
bar: u64, | ||
} | ||
|
||
#[must_use = "Baz"] | ||
pub union MustUseMessageUnionToMustUseMessageUnion { | ||
bar: u64, | ||
} | ||
|
||
|
||
// This union is private and should NOT be reported by this rule. | ||
|
||
#[must_use] | ||
union MustUsePrivateUnion { | ||
bar: u64, | ||
} | ||
|
||
|
||
// This union 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 union MustUseNewUnion { | ||
bar: u64, | ||
} |
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 = "union_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,51 @@ | ||
// These unions did not have the #[must_use] attribute in the old version. | ||
// Addition of the attribute should be reported by this rule. | ||
|
||
pub union UnionToMustUseUnion { | ||
bar: u64, | ||
} | ||
|
||
pub union UnionToMustUseMessageUnion { | ||
bar: u64, | ||
} | ||
|
||
|
||
// These unions 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 union MustUseUnionToUnion { | ||
bar: u64, | ||
} | ||
|
||
#[must_use] | ||
pub union MustUseUnionToMustUseMessageUnion { | ||
bar: u64, | ||
} | ||
|
||
|
||
// These unions 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 union MustUseMessageUnionToUnion { | ||
bar: u64, | ||
} | ||
|
||
#[must_use = "Foo"] | ||
pub union MustUseMessageUnionToMustUseUnion { | ||
bar: u64, | ||
} | ||
|
||
#[must_use = "Foo"] | ||
pub union MustUseMessageUnionToMustUseMessageUnion { | ||
bar: u64, | ||
} | ||
|
||
|
||
// This union is private and should NOT be reported by this rule. | ||
|
||
union MustUsePrivateUnion { | ||
bar: u64, | ||
} |
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/union_must_use_added/": [ | ||
{ | ||
"name": String("UnionToMustUseUnion"), | ||
"new_attr": String("#[must_use]"), | ||
"path": List([ | ||
String("union_must_use_added"), | ||
String("UnionToMustUseUnion"), | ||
]), | ||
"span_begin_line": Uint64(5), | ||
"span_filename": String("src/lib.rs"), | ||
"visibility_limit": String("public"), | ||
}, | ||
{ | ||
"name": String("UnionToMustUseMessageUnion"), | ||
"new_attr": String("#[must_use = \"Foo\"]"), | ||
"path": List([ | ||
String("union_must_use_added"), | ||
String("UnionToMustUseMessageUnion"), | ||
]), | ||
"span_begin_line": Uint64(10), | ||
"span_filename": String("src/lib.rs"), | ||
"visibility_limit": String("public"), | ||
}, | ||
], | ||
} |