Rollup merge of #145274 - compiler-errors:unused-must-use, r=fmease

Remove unused `#[must_use]`

Self-explanatory

Fixes https://github.com/rust-lang/rust/issues/145257
This commit is contained in:
Jakub Beránek
2025-08-13 07:03:49 +02:00
committed by GitHub
13 changed files with 366 additions and 206 deletions

View File

@@ -2155,6 +2155,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
attr_name, attr_name,
macro_name: pprust::path_to_string(&call.path), macro_name: pprust::path_to_string(&call.path),
invoc_span: call.path.span, invoc_span: call.path.span,
attr_span: attr.span,
}, },
); );
} }

View File

@@ -983,6 +983,7 @@ lint_unused_allocation_mut = unnecessary allocation, use `&mut` instead
lint_unused_builtin_attribute = unused attribute `{$attr_name}` lint_unused_builtin_attribute = unused attribute `{$attr_name}`
.note = the built-in attribute `{$attr_name}` will be ignored, since it's applied to the macro invocation `{$macro_name}` .note = the built-in attribute `{$attr_name}` will be ignored, since it's applied to the macro invocation `{$macro_name}`
.suggestion = remove the attribute
lint_unused_closure = lint_unused_closure =
unused {$pre}{$count -> unused {$pre}{$count ->

View File

@@ -205,8 +205,14 @@ pub fn decorate_builtin_lint(
} }
.decorate_lint(diag); .decorate_lint(diag);
} }
BuiltinLintDiag::UnusedBuiltinAttribute { attr_name, macro_name, invoc_span } => { BuiltinLintDiag::UnusedBuiltinAttribute {
lints::UnusedBuiltinAttribute { invoc_span, attr_name, macro_name }.decorate_lint(diag); attr_name,
macro_name,
invoc_span,
attr_span,
} => {
lints::UnusedBuiltinAttribute { invoc_span, attr_name, macro_name, attr_span }
.decorate_lint(diag);
} }
BuiltinLintDiag::TrailingMacro(is_trailing, name) => { BuiltinLintDiag::TrailingMacro(is_trailing, name) => {
lints::TrailingMacro { is_trailing, name }.decorate_lint(diag); lints::TrailingMacro { is_trailing, name }.decorate_lint(diag);

View File

@@ -2938,9 +2938,10 @@ pub(crate) struct RawPrefix {
pub(crate) struct UnusedBuiltinAttribute { pub(crate) struct UnusedBuiltinAttribute {
#[note] #[note]
pub invoc_span: Span, pub invoc_span: Span,
pub attr_name: Symbol, pub attr_name: Symbol,
pub macro_name: String, pub macro_name: String,
#[suggestion(code = "", applicability = "machine-applicable", style = "tool-only")]
pub attr_span: Span,
} }
#[derive(LintDiagnostic)] #[derive(LintDiagnostic)]

View File

@@ -647,6 +647,7 @@ pub enum BuiltinLintDiag {
attr_name: Symbol, attr_name: Symbol,
macro_name: String, macro_name: String,
invoc_span: Span, invoc_span: Span,
attr_span: Span,
}, },
PatternsInFnsWithoutBody { PatternsInFnsWithoutBody {
span: Span, span: Span,

View File

@@ -510,6 +510,7 @@ passes_must_not_suspend =
passes_must_use_no_effect = passes_must_use_no_effect =
`#[must_use]` has no effect when applied to {$article} {$target} `#[must_use]` has no effect when applied to {$article} {$target}
.suggestion = remove the attribute
passes_no_link = passes_no_link =
attribute should be applied to an `extern crate` item attribute should be applied to an `extern crate` item

View File

@@ -1622,7 +1622,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
UNUSED_ATTRIBUTES, UNUSED_ATTRIBUTES,
hir_id, hir_id,
attr_span, attr_span,
errors::MustUseNoEffect { article, target }, errors::MustUseNoEffect { article, target, attr_span },
); );
} }

View File

@@ -469,6 +469,8 @@ pub(crate) struct FfiConstInvalidTarget {
pub(crate) struct MustUseNoEffect { pub(crate) struct MustUseNoEffect {
pub article: &'static str, pub article: &'static str,
pub target: rustc_hir::Target, pub target: rustc_hir::Target,
#[suggestion(code = "", applicability = "machine-applicable", style = "tool-only")]
pub attr_span: Span,
} }
#[derive(Diagnostic)] #[derive(Diagnostic)]

View File

@@ -71,6 +71,7 @@
//~^^ WARN this was previously accepted by the compiler //~^^ WARN this was previously accepted by the compiler
#![must_use] #![must_use]
//~^ WARN `#[must_use]` has no effect //~^ WARN `#[must_use]` has no effect
//~| HELP remove the attribute
// see issue-43106-gating-of-stable.rs // see issue-43106-gating-of-stable.rs
// see issue-43106-gating-of-unstable.rs // see issue-43106-gating-of-unstable.rs
// see issue-43106-gating-of-deprecated.rs // see issue-43106-gating-of-deprecated.rs
@@ -599,16 +600,20 @@ mod deprecated {
} }
#[must_use] //~ WARN `#[must_use]` has no effect #[must_use] //~ WARN `#[must_use]` has no effect
//~^ HELP remove the attribute
mod must_use { mod must_use {
mod inner { #![must_use] } //~ WARN `#[must_use]` has no effect mod inner { #![must_use] } //~ WARN `#[must_use]` has no effect
//~^ HELP remove the attribute
#[must_use] fn f() { } #[must_use] fn f() { }
#[must_use] struct S; #[must_use] struct S;
#[must_use] type T = S; //~ WARN `#[must_use]` has no effect #[must_use] type T = S; //~ WARN `#[must_use]` has no effect
//~^ HELP remove the attribute
#[must_use] impl S { } //~ WARN `#[must_use]` has no effect #[must_use] impl S { } //~ WARN `#[must_use]` has no effect
//~^ HELP remove the attribute
} }
#[windows_subsystem = "windows"] #[windows_subsystem = "windows"]

View File

@@ -0,0 +1,139 @@
//@ run-rustfix
#![allow(dead_code, path_statements)]
#![deny(unused_attributes, unused_must_use)]
#![feature(asm_experimental_arch, stmt_expr_attributes, trait_alias)]
//~ ERROR `#[must_use]` has no effect
extern crate std as std2;
//~ ERROR `#[must_use]` has no effect
mod test_mod {}
//~ ERROR `#[must_use]` has no effect
use std::arch::global_asm;
//~ ERROR `#[must_use]` has no effect
const CONST: usize = 4;
//~ ERROR `#[must_use]` has no effect
#[no_mangle]
static STATIC: usize = 4;
#[must_use]
struct X;
#[must_use]
enum Y {
Z,
}
#[must_use]
union U {
unit: (),
}
//~ ERROR `#[must_use]` has no effect
impl U {
#[must_use]
fn method() -> i32 {
4
}
}
#[must_use]
#[no_mangle]
fn foo() -> i64 {
4
}
//~ ERROR `#[must_use]` has no effect
extern "Rust" {
#[link_name = "STATIC"]
//~ ERROR `#[must_use]` has no effect
static FOREIGN_STATIC: usize;
#[link_name = "foo"]
#[must_use]
fn foreign_foo() -> i64;
}
//~ ERROR unused attribute
global_asm!("");
//~ ERROR `#[must_use]` has no effect
type UseMe = ();
fn qux< T>(_: T) {} //~ ERROR `#[must_use]` has no effect
#[must_use]
trait Use {
//~ ERROR `#[must_use]` has no effect
const ASSOC_CONST: usize = 4;
//~ ERROR `#[must_use]` has no effect
type AssocTy;
#[must_use]
fn get_four(&self) -> usize {
4
}
}
//~ ERROR `#[must_use]` has no effect
impl Use for () {
type AssocTy = ();
//~ ERROR `#[must_use]` has no effect
fn get_four(&self) -> usize {
4
}
}
//~ ERROR `#[must_use]` has no effect
trait Alias = Use;
//~ ERROR `#[must_use]` has no effect
macro_rules! cool_macro {
() => {
4
};
}
fn main() {
//~ ERROR `#[must_use]` has no effect
let x = || {};
x();
let x = //~ ERROR `#[must_use]` has no effect
|| {};
x();
let _ = X; //~ ERROR that must be used
let _ = Y::Z; //~ ERROR that must be used
let _ = U { unit: () }; //~ ERROR that must be used
let _ = U::method(); //~ ERROR that must be used
let _ = foo(); //~ ERROR that must be used
unsafe {
let _ = foreign_foo(); //~ ERROR that must be used
};
CONST;
STATIC;
unsafe { FOREIGN_STATIC };
cool_macro!();
qux(4);
let _ = ().get_four(); //~ ERROR that must be used
match Some(4) {
//~ ERROR `#[must_use]` has no effect
Some(res) => res,
None => 0,
};
struct PatternField {
foo: i32,
}
let s = PatternField { foo: 123 }; //~ ERROR `#[must_use]` has no effect
let PatternField { foo } = s; //~ ERROR `#[must_use]` has no effect
let _ = foo;
}

View File

@@ -1,3 +1,5 @@
//@ run-rustfix
#![allow(dead_code, path_statements)] #![allow(dead_code, path_statements)]
#![deny(unused_attributes, unused_must_use)] #![deny(unused_attributes, unused_must_use)]
#![feature(asm_experimental_arch, stmt_expr_attributes, trait_alias)] #![feature(asm_experimental_arch, stmt_expr_attributes, trait_alias)]
@@ -133,4 +135,5 @@ fn main() {
} }
let s = PatternField { #[must_use] foo: 123 }; //~ ERROR `#[must_use]` has no effect let s = PatternField { #[must_use] foo: 123 }; //~ ERROR `#[must_use]` has no effect
let PatternField { #[must_use] foo } = s; //~ ERROR `#[must_use]` has no effect let PatternField { #[must_use] foo } = s; //~ ERROR `#[must_use]` has no effect
let _ = foo;
} }

View File

@@ -1,154 +1,154 @@
error: unused attribute `must_use` error: unused attribute `must_use`
--> $DIR/unused_attributes-must_use.rs:58:1 --> $DIR/unused_attributes-must_use.rs:60:1
| |
LL | #[must_use] LL | #[must_use]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
| |
note: the built-in attribute `must_use` will be ignored, since it's applied to the macro invocation `global_asm` note: the built-in attribute `must_use` will be ignored, since it's applied to the macro invocation `global_asm`
--> $DIR/unused_attributes-must_use.rs:59:1 --> $DIR/unused_attributes-must_use.rs:61:1
| |
LL | global_asm!(""); LL | global_asm!("");
| ^^^^^^^^^^ | ^^^^^^^^^^
note: the lint level is defined here note: the lint level is defined here
--> $DIR/unused_attributes-must_use.rs:2:9 --> $DIR/unused_attributes-must_use.rs:4:9
| |
LL | #![deny(unused_attributes, unused_must_use)] LL | #![deny(unused_attributes, unused_must_use)]
| ^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to an extern crate error: `#[must_use]` has no effect when applied to an extern crate
--> $DIR/unused_attributes-must_use.rs:5:1 --> $DIR/unused_attributes-must_use.rs:7:1
| |
LL | #[must_use] LL | #[must_use]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to a module error: `#[must_use]` has no effect when applied to a module
--> $DIR/unused_attributes-must_use.rs:8:1 --> $DIR/unused_attributes-must_use.rs:10:1
| |
LL | #[must_use] LL | #[must_use]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to a use error: `#[must_use]` has no effect when applied to a use
--> $DIR/unused_attributes-must_use.rs:11:1 --> $DIR/unused_attributes-must_use.rs:13:1
| |
LL | #[must_use] LL | #[must_use]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to a constant item error: `#[must_use]` has no effect when applied to a constant item
--> $DIR/unused_attributes-must_use.rs:14:1
|
LL | #[must_use]
| ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to a static item
--> $DIR/unused_attributes-must_use.rs:16:1 --> $DIR/unused_attributes-must_use.rs:16:1
| |
LL | #[must_use] LL | #[must_use]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to a static item
--> $DIR/unused_attributes-must_use.rs:18:1
|
LL | #[must_use]
| ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to an inherent implementation block error: `#[must_use]` has no effect when applied to an inherent implementation block
--> $DIR/unused_attributes-must_use.rs:33:1 --> $DIR/unused_attributes-must_use.rs:35:1
| |
LL | #[must_use] LL | #[must_use]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to a foreign module error: `#[must_use]` has no effect when applied to a foreign module
--> $DIR/unused_attributes-must_use.rs:47:1 --> $DIR/unused_attributes-must_use.rs:49:1
| |
LL | #[must_use] LL | #[must_use]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to a type alias error: `#[must_use]` has no effect when applied to a type alias
--> $DIR/unused_attributes-must_use.rs:61:1 --> $DIR/unused_attributes-must_use.rs:63:1
| |
LL | #[must_use] LL | #[must_use]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to a type parameter error: `#[must_use]` has no effect when applied to a type parameter
--> $DIR/unused_attributes-must_use.rs:64:8 --> $DIR/unused_attributes-must_use.rs:66:8
| |
LL | fn qux<#[must_use] T>(_: T) {} LL | fn qux<#[must_use] T>(_: T) {}
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to an trait implementation block error: `#[must_use]` has no effect when applied to an trait implementation block
--> $DIR/unused_attributes-must_use.rs:79:1 --> $DIR/unused_attributes-must_use.rs:81:1
| |
LL | #[must_use] LL | #[must_use]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to a trait alias error: `#[must_use]` has no effect when applied to a trait alias
--> $DIR/unused_attributes-must_use.rs:89:1 --> $DIR/unused_attributes-must_use.rs:91:1
| |
LL | #[must_use] LL | #[must_use]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to a macro def error: `#[must_use]` has no effect when applied to a macro def
--> $DIR/unused_attributes-must_use.rs:92:1 --> $DIR/unused_attributes-must_use.rs:94:1
| |
LL | #[must_use] LL | #[must_use]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to a statement error: `#[must_use]` has no effect when applied to a statement
--> $DIR/unused_attributes-must_use.rs:100:5 --> $DIR/unused_attributes-must_use.rs:102:5
| |
LL | #[must_use] LL | #[must_use]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to a closure error: `#[must_use]` has no effect when applied to a closure
--> $DIR/unused_attributes-must_use.rs:104:13 --> $DIR/unused_attributes-must_use.rs:106:13
| |
LL | let x = #[must_use] LL | let x = #[must_use]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to an match arm error: `#[must_use]` has no effect when applied to an match arm
--> $DIR/unused_attributes-must_use.rs:126:9 --> $DIR/unused_attributes-must_use.rs:128:9
| |
LL | #[must_use] LL | #[must_use]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to a struct field error: `#[must_use]` has no effect when applied to a struct field
--> $DIR/unused_attributes-must_use.rs:134:28 --> $DIR/unused_attributes-must_use.rs:136:28
| |
LL | let s = PatternField { #[must_use] foo: 123 }; LL | let s = PatternField { #[must_use] foo: 123 };
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to a pattern field error: `#[must_use]` has no effect when applied to a pattern field
--> $DIR/unused_attributes-must_use.rs:135:24 --> $DIR/unused_attributes-must_use.rs:137:24
| |
LL | let PatternField { #[must_use] foo } = s; LL | let PatternField { #[must_use] foo } = s;
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to an associated const error: `#[must_use]` has no effect when applied to an associated const
--> $DIR/unused_attributes-must_use.rs:68:5
|
LL | #[must_use]
| ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to an associated type
--> $DIR/unused_attributes-must_use.rs:70:5 --> $DIR/unused_attributes-must_use.rs:70:5
| |
LL | #[must_use] LL | #[must_use]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to an associated type
--> $DIR/unused_attributes-must_use.rs:72:5
|
LL | #[must_use]
| ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to a provided trait method error: `#[must_use]` has no effect when applied to a provided trait method
--> $DIR/unused_attributes-must_use.rs:83:5 --> $DIR/unused_attributes-must_use.rs:85:5
| |
LL | #[must_use] LL | #[must_use]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: `#[must_use]` has no effect when applied to a foreign static item error: `#[must_use]` has no effect when applied to a foreign static item
--> $DIR/unused_attributes-must_use.rs:50:5 --> $DIR/unused_attributes-must_use.rs:52:5
| |
LL | #[must_use] LL | #[must_use]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: unused `X` that must be used error: unused `X` that must be used
--> $DIR/unused_attributes-must_use.rs:108:5 --> $DIR/unused_attributes-must_use.rs:110:5
| |
LL | X; LL | X;
| ^ | ^
| |
note: the lint level is defined here note: the lint level is defined here
--> $DIR/unused_attributes-must_use.rs:2:28 --> $DIR/unused_attributes-must_use.rs:4:28
| |
LL | #![deny(unused_attributes, unused_must_use)] LL | #![deny(unused_attributes, unused_must_use)]
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
@@ -158,7 +158,7 @@ LL | let _ = X;
| +++++++ | +++++++
error: unused `Y` that must be used error: unused `Y` that must be used
--> $DIR/unused_attributes-must_use.rs:109:5 --> $DIR/unused_attributes-must_use.rs:111:5
| |
LL | Y::Z; LL | Y::Z;
| ^^^^ | ^^^^
@@ -169,7 +169,7 @@ LL | let _ = Y::Z;
| +++++++ | +++++++
error: unused `U` that must be used error: unused `U` that must be used
--> $DIR/unused_attributes-must_use.rs:110:5 --> $DIR/unused_attributes-must_use.rs:112:5
| |
LL | U { unit: () }; LL | U { unit: () };
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
@@ -180,7 +180,7 @@ LL | let _ = U { unit: () };
| +++++++ | +++++++
error: unused return value of `U::method` that must be used error: unused return value of `U::method` that must be used
--> $DIR/unused_attributes-must_use.rs:111:5 --> $DIR/unused_attributes-must_use.rs:113:5
| |
LL | U::method(); LL | U::method();
| ^^^^^^^^^^^ | ^^^^^^^^^^^
@@ -191,7 +191,7 @@ LL | let _ = U::method();
| +++++++ | +++++++
error: unused return value of `foo` that must be used error: unused return value of `foo` that must be used
--> $DIR/unused_attributes-must_use.rs:112:5 --> $DIR/unused_attributes-must_use.rs:114:5
| |
LL | foo(); LL | foo();
| ^^^^^ | ^^^^^
@@ -202,7 +202,7 @@ LL | let _ = foo();
| +++++++ | +++++++
error: unused return value of `foreign_foo` that must be used error: unused return value of `foreign_foo` that must be used
--> $DIR/unused_attributes-must_use.rs:115:9 --> $DIR/unused_attributes-must_use.rs:117:9
| |
LL | foreign_foo(); LL | foreign_foo();
| ^^^^^^^^^^^^^ | ^^^^^^^^^^^^^
@@ -213,7 +213,7 @@ LL | let _ = foreign_foo();
| +++++++ | +++++++
error: unused return value of `Use::get_four` that must be used error: unused return value of `Use::get_four` that must be used
--> $DIR/unused_attributes-must_use.rs:123:5 --> $DIR/unused_attributes-must_use.rs:125:5
| |
LL | ().get_four(); LL | ().get_four();
| ^^^^^^^^^^^^^ | ^^^^^^^^^^^^^