Skip to content

Commit

Permalink
Adds union_must_use_added lint (#845)
Browse files Browse the repository at this point in the history
* 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
PedroTurik and obi1kenobi authored Aug 1, 2024
1 parent 58d76d6 commit 941e14c
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ inherent_method_must_use_added = "warn"
struct_must_use_added = "warn"
enum_must_use_added = "warn"
trait_must_use_added = "warn"
union_must_use_added = "warn"
```

#### Common configurations: Disable `#[must_use]` lints entirely
Expand All @@ -428,6 +429,7 @@ inherent_method_must_use_added = "allow"
struct_must_use_added = "allow"
enum_must_use_added = "allow"
trait_must_use_added = "allow"
union_must_use_added = "allow"
```

#### Implementation details & limitations
Expand Down
64 changes: 64 additions & 0 deletions src/lints/union_must_use_added.ron
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}}"),
)
1 change: 1 addition & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,7 @@ add_lints!(
type_marked_deprecated,
union_field_missing,
union_missing,
union_must_use_added,
union_now_doc_hidden,
union_pub_field_now_doc_hidden,
unit_struct_changed_kind,
Expand Down
7 changes: 7 additions & 0 deletions test_crates/union_must_use_added/new/Cargo.toml
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]
62 changes: 62 additions & 0 deletions test_crates/union_must_use_added/new/src/lib.rs
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,
}
7 changes: 7 additions & 0 deletions test_crates/union_must_use_added/old/Cargo.toml
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]
51 changes: 51 additions & 0 deletions test_crates/union_must_use_added/old/src/lib.rs
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,
}
26 changes: 26 additions & 0 deletions test_outputs/union_must_use_added.output.ron
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"),
},
],
}

0 comments on commit 941e14c

Please sign in to comment.