Skip to content

Simplify must_use_candidate spans #15310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions clippy_lints/src/functions/must_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_span::{Span, sym};

use clippy_utils::attrs::is_proc_macro;
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_then};
use clippy_utils::source::SpanRangeExt;
use clippy_utils::source::snippet_indent;
use clippy_utils::ty::is_must_use_ty;
use clippy_utils::visitors::for_each_expr_without_closures;
use clippy_utils::{return_ty, trait_ref_of_method};
Expand All @@ -28,6 +28,7 @@ pub(super) fn check_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>
if let hir::ItemKind::Fn {
ref sig,
body: ref body_id,
ident,
..
} = item.kind
{
Expand All @@ -51,8 +52,8 @@ pub(super) fn check_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>
sig.decl,
cx.tcx.hir_body(*body_id),
item.span,
ident.span,
item.owner_id,
item.span.with_hi(sig.decl.output.span().hi()),
"this function could have a `#[must_use]` attribute",
);
}
Expand Down Expand Up @@ -84,8 +85,8 @@ pub(super) fn check_impl_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Imp
sig.decl,
cx.tcx.hir_body(*body_id),
item.span,
item.ident.span,
item.owner_id,
item.span.with_hi(sig.decl.output.span().hi()),
"this method could have a `#[must_use]` attribute",
);
}
Expand Down Expand Up @@ -120,8 +121,8 @@ pub(super) fn check_trait_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Tr
sig.decl,
body,
item.span,
item.ident.span,
item.owner_id,
item.span.with_hi(sig.decl.output.span().hi()),
"this method could have a `#[must_use]` attribute",
);
}
Expand Down Expand Up @@ -198,8 +199,8 @@ fn check_must_use_candidate<'tcx>(
decl: &'tcx hir::FnDecl<'_>,
body: &'tcx hir::Body<'_>,
item_span: Span,
ident_span: Span,
item_id: hir::OwnerId,
fn_span: Span,
msg: &'static str,
) {
if has_mutable_arg(cx, body)
Expand All @@ -208,18 +209,18 @@ fn check_must_use_candidate<'tcx>(
|| returns_unit(decl)
|| !cx.effective_visibilities.is_exported(item_id.def_id)
|| is_must_use_ty(cx, return_ty(cx, item_id))
|| item_span.from_expansion()
{
return;
}
span_lint_and_then(cx, MUST_USE_CANDIDATE, fn_span, msg, |diag| {
if let Some(snippet) = fn_span.get_source_text(cx) {
diag.span_suggestion(
fn_span,
"add the attribute",
format!("#[must_use] {snippet}"),
Applicability::MachineApplicable,
);
}
span_lint_and_then(cx, MUST_USE_CANDIDATE, ident_span, msg, |diag| {
let indent = snippet_indent(cx, item_span).unwrap_or_default();
diag.span_suggestion(
item_span.shrink_to_lo(),
"add the attribute",
format!("#[must_use] \n{indent}"),
Applicability::MachineApplicable,
);
});
}

Expand Down
15 changes: 10 additions & 5 deletions tests/ui/must_use_candidates.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ use std::sync::atomic::{AtomicBool, Ordering};
pub struct MyAtomic(AtomicBool);
pub struct MyPure;

#[must_use] pub fn pure(i: u8) -> u8 {
#[must_use]
pub fn pure(i: u8) -> u8 {
//~^ must_use_candidate
i
}

impl MyPure {
#[must_use] pub fn inherent_pure(&self) -> u8 {
#[must_use]
pub fn inherent_pure(&self) -> u8 {
//~^ must_use_candidate
0
}
Expand Down Expand Up @@ -51,7 +53,8 @@ pub fn with_callback<F: Fn(u32) -> bool>(f: &F) -> bool {
f(0)
}

#[must_use] pub fn with_marker(_d: std::marker::PhantomData<&mut u32>) -> bool {
#[must_use]
pub fn with_marker(_d: std::marker::PhantomData<&mut u32>) -> bool {
//~^ must_use_candidate
true
}
Expand All @@ -64,7 +67,8 @@ pub fn atomics(b: &AtomicBool) -> bool {
b.load(Ordering::SeqCst)
}

#[must_use] pub fn rcd(_x: Rc<u32>) -> bool {
#[must_use]
pub fn rcd(_x: Rc<u32>) -> bool {
//~^ must_use_candidate
true
}
Expand All @@ -73,7 +77,8 @@ pub fn rcmut(_x: Rc<&mut u32>) -> bool {
true
}

#[must_use] pub fn arcd(_x: Arc<u32>) -> bool {
#[must_use]
pub fn arcd(_x: Arc<u32>) -> bool {
//~^ must_use_candidate
false
}
Expand Down
49 changes: 39 additions & 10 deletions tests/ui/must_use_candidates.stderr
Original file line number Diff line number Diff line change
@@ -1,35 +1,64 @@
error: this function could have a `#[must_use]` attribute
--> tests/ui/must_use_candidates.rs:16:1
--> tests/ui/must_use_candidates.rs:16:8
|
LL | pub fn pure(i: u8) -> u8 {
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn pure(i: u8) -> u8`
| ^^^^
|
= note: `-D clippy::must-use-candidate` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::must_use_candidate)]`
help: add the attribute
|
LL + #[must_use]
LL | pub fn pure(i: u8) -> u8 {
|

error: this method could have a `#[must_use]` attribute
--> tests/ui/must_use_candidates.rs:22:5
--> tests/ui/must_use_candidates.rs:22:12
|
LL | pub fn inherent_pure(&self) -> u8 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn inherent_pure(&self) -> u8`
| ^^^^^^^^^^^^^
|
help: add the attribute
|
LL ~ #[must_use]
LL ~ pub fn inherent_pure(&self) -> u8 {
|

error: this function could have a `#[must_use]` attribute
--> tests/ui/must_use_candidates.rs:54:1
--> tests/ui/must_use_candidates.rs:54:8
|
LL | pub fn with_marker(_d: std::marker::PhantomData<&mut u32>) -> bool {
| ^^^^^^^^^^^
|
help: add the attribute
|
LL + #[must_use]
LL | pub fn with_marker(_d: std::marker::PhantomData<&mut u32>) -> bool {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn with_marker(_d: std::marker::PhantomData<&mut u32>) -> bool`
|

error: this function could have a `#[must_use]` attribute
--> tests/ui/must_use_candidates.rs:67:1
--> tests/ui/must_use_candidates.rs:67:8
|
LL | pub fn rcd(_x: Rc<u32>) -> bool {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn rcd(_x: Rc<u32>) -> bool`
| ^^^
|
help: add the attribute
|
LL + #[must_use]
LL | pub fn rcd(_x: Rc<u32>) -> bool {
|

error: this function could have a `#[must_use]` attribute
--> tests/ui/must_use_candidates.rs:76:1
--> tests/ui/must_use_candidates.rs:76:8
|
LL | pub fn arcd(_x: Arc<u32>) -> bool {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn arcd(_x: Arc<u32>) -> bool`
| ^^^^
|
help: add the attribute
|
LL + #[must_use]
LL | pub fn arcd(_x: Arc<u32>) -> bool {
|

error: aborting due to 5 previous errors