Skip to content

Commit cca924b

Browse files
committed
Simplify must_use_candidate spans.
1 parent e62e27b commit cca924b

File tree

3 files changed

+64
-29
lines changed

3 files changed

+64
-29
lines changed

clippy_lints/src/functions/must_use.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_span::{Span, sym};
1010

1111
use clippy_utils::attrs::is_proc_macro;
1212
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_then};
13-
use clippy_utils::source::SpanRangeExt;
13+
use clippy_utils::source::snippet_indent;
1414
use clippy_utils::ty::is_must_use_ty;
1515
use clippy_utils::visitors::for_each_expr_without_closures;
1616
use clippy_utils::{return_ty, trait_ref_of_method};
@@ -28,6 +28,7 @@ pub(super) fn check_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>
2828
if let hir::ItemKind::Fn {
2929
ref sig,
3030
body: ref body_id,
31+
ident,
3132
..
3233
} = item.kind
3334
{
@@ -51,8 +52,8 @@ pub(super) fn check_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>
5152
sig.decl,
5253
cx.tcx.hir_body(*body_id),
5354
item.span,
55+
ident.span,
5456
item.owner_id,
55-
item.span.with_hi(sig.decl.output.span().hi()),
5657
"this function could have a `#[must_use]` attribute",
5758
);
5859
}
@@ -84,8 +85,8 @@ pub(super) fn check_impl_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Imp
8485
sig.decl,
8586
cx.tcx.hir_body(*body_id),
8687
item.span,
88+
item.ident.span,
8789
item.owner_id,
88-
item.span.with_hi(sig.decl.output.span().hi()),
8990
"this method could have a `#[must_use]` attribute",
9091
);
9192
}
@@ -120,8 +121,8 @@ pub(super) fn check_trait_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Tr
120121
sig.decl,
121122
body,
122123
item.span,
124+
item.ident.span,
123125
item.owner_id,
124-
item.span.with_hi(sig.decl.output.span().hi()),
125126
"this method could have a `#[must_use]` attribute",
126127
);
127128
}
@@ -198,8 +199,8 @@ fn check_must_use_candidate<'tcx>(
198199
decl: &'tcx hir::FnDecl<'_>,
199200
body: &'tcx hir::Body<'_>,
200201
item_span: Span,
202+
ident_span: Span,
201203
item_id: hir::OwnerId,
202-
fn_span: Span,
203204
msg: &'static str,
204205
) {
205206
if has_mutable_arg(cx, body)
@@ -208,18 +209,18 @@ fn check_must_use_candidate<'tcx>(
208209
|| returns_unit(decl)
209210
|| !cx.effective_visibilities.is_exported(item_id.def_id)
210211
|| is_must_use_ty(cx, return_ty(cx, item_id))
212+
|| item_span.from_expansion()
211213
{
212214
return;
213215
}
214-
span_lint_and_then(cx, MUST_USE_CANDIDATE, fn_span, msg, |diag| {
215-
if let Some(snippet) = fn_span.get_source_text(cx) {
216-
diag.span_suggestion(
217-
fn_span,
218-
"add the attribute",
219-
format!("#[must_use] {snippet}"),
220-
Applicability::MachineApplicable,
221-
);
222-
}
216+
span_lint_and_then(cx, MUST_USE_CANDIDATE, ident_span, msg, |diag| {
217+
let indent = snippet_indent(cx, item_span).unwrap_or_default();
218+
diag.span_suggestion(
219+
item_span.shrink_to_lo(),
220+
"add the attribute",
221+
format!("#[must_use] \n{indent}"),
222+
Applicability::MachineApplicable,
223+
);
223224
});
224225
}
225226

tests/ui/must_use_candidates.fixed

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ use std::sync::atomic::{AtomicBool, Ordering};
1313
pub struct MyAtomic(AtomicBool);
1414
pub struct MyPure;
1515

16-
#[must_use] pub fn pure(i: u8) -> u8 {
16+
#[must_use]
17+
pub fn pure(i: u8) -> u8 {
1718
//~^ must_use_candidate
1819
i
1920
}
2021

2122
impl MyPure {
22-
#[must_use] pub fn inherent_pure(&self) -> u8 {
23+
#[must_use]
24+
pub fn inherent_pure(&self) -> u8 {
2325
//~^ must_use_candidate
2426
0
2527
}
@@ -51,7 +53,8 @@ pub fn with_callback<F: Fn(u32) -> bool>(f: &F) -> bool {
5153
f(0)
5254
}
5355

54-
#[must_use] pub fn with_marker(_d: std::marker::PhantomData<&mut u32>) -> bool {
56+
#[must_use]
57+
pub fn with_marker(_d: std::marker::PhantomData<&mut u32>) -> bool {
5558
//~^ must_use_candidate
5659
true
5760
}
@@ -64,7 +67,8 @@ pub fn atomics(b: &AtomicBool) -> bool {
6467
b.load(Ordering::SeqCst)
6568
}
6669

67-
#[must_use] pub fn rcd(_x: Rc<u32>) -> bool {
70+
#[must_use]
71+
pub fn rcd(_x: Rc<u32>) -> bool {
6872
//~^ must_use_candidate
6973
true
7074
}
@@ -73,7 +77,8 @@ pub fn rcmut(_x: Rc<&mut u32>) -> bool {
7377
true
7478
}
7579

76-
#[must_use] pub fn arcd(_x: Arc<u32>) -> bool {
80+
#[must_use]
81+
pub fn arcd(_x: Arc<u32>) -> bool {
7782
//~^ must_use_candidate
7883
false
7984
}

tests/ui/must_use_candidates.stderr

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,64 @@
11
error: this function could have a `#[must_use]` attribute
2-
--> tests/ui/must_use_candidates.rs:16:1
2+
--> tests/ui/must_use_candidates.rs:16:8
33
|
44
LL | pub fn pure(i: u8) -> u8 {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn pure(i: u8) -> u8`
5+
| ^^^^
66
|
77
= note: `-D clippy::must-use-candidate` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::must_use_candidate)]`
9+
help: add the attribute
10+
|
11+
LL + #[must_use]
12+
LL | pub fn pure(i: u8) -> u8 {
13+
|
914

1015
error: this method could have a `#[must_use]` attribute
11-
--> tests/ui/must_use_candidates.rs:22:5
16+
--> tests/ui/must_use_candidates.rs:22:12
1217
|
1318
LL | pub fn inherent_pure(&self) -> u8 {
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn inherent_pure(&self) -> u8`
19+
| ^^^^^^^^^^^^^
20+
|
21+
help: add the attribute
22+
|
23+
LL ~ #[must_use]
24+
LL ~ pub fn inherent_pure(&self) -> u8 {
25+
|
1526

1627
error: this function could have a `#[must_use]` attribute
17-
--> tests/ui/must_use_candidates.rs:54:1
28+
--> tests/ui/must_use_candidates.rs:54:8
29+
|
30+
LL | pub fn with_marker(_d: std::marker::PhantomData<&mut u32>) -> bool {
31+
| ^^^^^^^^^^^
32+
|
33+
help: add the attribute
1834
|
35+
LL + #[must_use]
1936
LL | pub fn with_marker(_d: std::marker::PhantomData<&mut u32>) -> bool {
20-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn with_marker(_d: std::marker::PhantomData<&mut u32>) -> bool`
37+
|
2138

2239
error: this function could have a `#[must_use]` attribute
23-
--> tests/ui/must_use_candidates.rs:67:1
40+
--> tests/ui/must_use_candidates.rs:67:8
2441
|
2542
LL | pub fn rcd(_x: Rc<u32>) -> bool {
26-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn rcd(_x: Rc<u32>) -> bool`
43+
| ^^^
44+
|
45+
help: add the attribute
46+
|
47+
LL + #[must_use]
48+
LL | pub fn rcd(_x: Rc<u32>) -> bool {
49+
|
2750

2851
error: this function could have a `#[must_use]` attribute
29-
--> tests/ui/must_use_candidates.rs:76:1
52+
--> tests/ui/must_use_candidates.rs:76:8
3053
|
3154
LL | pub fn arcd(_x: Arc<u32>) -> bool {
32-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn arcd(_x: Arc<u32>) -> bool`
55+
| ^^^^
56+
|
57+
help: add the attribute
58+
|
59+
LL + #[must_use]
60+
LL | pub fn arcd(_x: Arc<u32>) -> bool {
61+
|
3362

3463
error: aborting due to 5 previous errors
3564

0 commit comments

Comments
 (0)