Skip to content

Commit 80314b1

Browse files
committed
simplify must-use lint slightly
1 parent f321f10 commit 80314b1

File tree

6 files changed

+47
-52
lines changed

6 files changed

+47
-52
lines changed

Diff for: src/tools/clippy/clippy_lints/src/functions/must_use.rs

+7-23
Original file line numberDiff line numberDiff line change
@@ -124,32 +124,16 @@ fn check_needless_must_use(
124124
} else {
125125
// When there are multiple attributes, it is not sufficient to simply make `must_use` empty, see
126126
// issue #12320.
127-
span_lint_and_then(
127+
// FIXME(jdonszelmann): this used to give a machine-applicable fix. However, it was super fragile,
128+
// honestly looked incorrect, and is a little hard to support for a little bit now. Some day this could be
129+
// re-added.
130+
span_lint_and_help(
128131
cx,
129-
MUST_USE_UNIT,
132+
DOUBLE_MUST_USE,
130133
fn_header_span,
131134
"this unit-returning function has a `#[must_use]` attribute",
132-
|diag| {
133-
let mut attrs_without_must_use = attrs.to_vec();
134-
attrs_without_must_use.retain(|a| a.id() != attr.id());
135-
let sugg_str = attrs_without_must_use
136-
.iter()
137-
.map(|a| {
138-
if a.value_str().is_none() {
139-
return a.name_or_empty().to_string();
140-
}
141-
format!("{} = \"{}\"", a.name_or_empty(), a.value_str().unwrap())
142-
})
143-
.collect::<Vec<_>>()
144-
.join(", ");
145-
146-
diag.span_suggestion(
147-
attrs[0].span().with_hi(attrs[attrs.len() - 1].span().hi()),
148-
"change these attributes to",
149-
sugg_str,
150-
Applicability::MachineApplicable,
151-
);
152-
},
135+
Some(attr.span()),
136+
"remove `must_use`",
153137
);
154138
}
155139
} else if attr.value_str().is_none() && is_must_use_ty(cx, return_ty(cx, item_id)) {

Diff for: src/tools/clippy/tests/ui/must_use_unit.fixed

-5
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,3 @@ fn main() {
2424
);
2525
}
2626

27-
#[cfg_attr(all(), )]
28-
fn issue_12320() {}
29-
30-
#[cfg_attr(all(), deprecateddoc = "foo", doc = "foo", must_use)]
31-
fn issue_12320_2() {}

Diff for: src/tools/clippy/tests/ui/must_use_unit.rs

-5
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,3 @@ fn main() {
2727
);
2828
}
2929

30-
#[cfg_attr(all(), must_use, deprecated)]
31-
fn issue_12320() {}
32-
33-
#[cfg_attr(all(), deprecated, doc = "foo", must_use)]
34-
fn issue_12320_2() {}

Diff for: src/tools/clippy/tests/ui/must_use_unit.stderr

+1-19
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,5 @@ LL | #[must_use = "With note"]
2525
LL | pub fn must_use_with_note() {}
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2727

28-
[Unparsed(AttrItem { path: AttrPath { segments: [must_use#0], span: tests/ui/must_use_unit.rs:30:19: 30:27 (#0) }, args: Empty, id: HashIgnoredAttrId { attr_id: AttrId(9) }, style: Outer, span: tests/ui/must_use_unit.rs:30:19: 30:27 (#0) }), Parsed(Deprecation { deprecation: Deprecation { since: Unspecified, note: None, suggestion: None }, span: tests/ui/must_use_unit.rs:30:29: 30:39 (#0) })]
29-
error: this unit-returning function has a `#[must_use]` attribute
30-
--> tests/ui/must_use_unit.rs:31:1
31-
|
32-
LL | #[cfg_attr(all(), must_use, deprecated)]
33-
| -------------------- help: change these attributes to
34-
LL | fn issue_12320() {}
35-
| ^^^^^^^^^^^^^^^^
36-
37-
[Unparsed(AttrItem { path: AttrPath { segments: [doc#0], span: tests/ui/must_use_unit.rs:33:31: 33:34 (#0) }, args: Eq { eq_span: tests/ui/must_use_unit.rs:33:35: 33:36 (#0), expr: MetaItemLit { symbol: "foo", suffix: None, kind: Str("foo", Cooked), span: tests/ui/must_use_unit.rs:33:37: 33:42 (#0) } }, id: HashIgnoredAttrId { attr_id: AttrId(12) }, style: Outer, span: tests/ui/must_use_unit.rs:33:31: 33:42 (#0) }), Unparsed(AttrItem { path: AttrPath { segments: [must_use#0], span: tests/ui/must_use_unit.rs:33:44: 33:52 (#0) }, args: Empty, id: HashIgnoredAttrId { attr_id: AttrId(13) }, style: Outer, span: tests/ui/must_use_unit.rs:33:44: 33:52 (#0) }), Parsed(Deprecation { deprecation: Deprecation { since: Unspecified, note: None, suggestion: None }, span: tests/ui/must_use_unit.rs:33:19: 33:29 (#0) })]
38-
error: this unit-returning function has a `#[must_use]` attribute
39-
--> tests/ui/must_use_unit.rs:34:1
40-
|
41-
LL | #[cfg_attr(all(), deprecated, doc = "foo", must_use)]
42-
| -- help: change these attributes to: `doc = "foo",`
43-
LL | fn issue_12320_2() {}
44-
| ^^^^^^^^^^^^^^^^^^
45-
46-
error: aborting due to 5 previous errors
28+
error: aborting due to 3 previous errors
4729

Diff for: src/tools/clippy/tests/ui/must_use_unit_12320.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@aux-build:proc_macros.rs
2+
//@no-rustfix
3+
4+
#![warn(clippy::must_use_unit)]
5+
#![allow(clippy::unused_unit)]
6+
7+
#[cfg_attr(all(), must_use, deprecated)]
8+
fn issue_12320() {}
9+
10+
#[cfg_attr(all(), deprecated, doc = "foo", must_use)]
11+
fn issue_12320_2() {}

Diff for: src/tools/clippy/tests/ui/must_use_unit_12320.stderr

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: this unit-returning function has a `#[must_use]` attribute
2+
--> tests/ui/must_use_unit_12320.rs:8:1
3+
|
4+
LL | fn issue_12320() {}
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
help: remove `must_use`
8+
--> tests/ui/must_use_unit_12320.rs:7:19
9+
|
10+
LL | #[cfg_attr(all(), must_use, deprecated)]
11+
| ^^^^^^^^
12+
= note: `-D clippy::double-must-use` implied by `-D warnings`
13+
= help: to override `-D warnings` add `#[allow(clippy::double_must_use)]`
14+
15+
error: this unit-returning function has a `#[must_use]` attribute
16+
--> tests/ui/must_use_unit_12320.rs:11:1
17+
|
18+
LL | fn issue_12320_2() {}
19+
| ^^^^^^^^^^^^^^^^^^
20+
|
21+
help: remove `must_use`
22+
--> tests/ui/must_use_unit_12320.rs:10:44
23+
|
24+
LL | #[cfg_attr(all(), deprecated, doc = "foo", must_use)]
25+
| ^^^^^^^^
26+
27+
error: aborting due to 2 previous errors
28+

0 commit comments

Comments
 (0)