Skip to content

Remove unused #[must_use] #145274

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

compiler-errors
Copy link
Member

Self-explanatory

Fixes #145257

@rustbot
Copy link
Collaborator

rustbot commented Aug 11, 2025

r? @wesleywiser

rustbot has assigned @wesleywiser.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 11, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 11, 2025

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann

@@ -133,4 +135,5 @@ fn main() {
}
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 _ = foo;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@run-rustfix requires that all warnings are gone, and foo is unused, so I'm suppressing it here.

@fmease
Copy link
Member

fmease commented Aug 11, 2025

Thenks!~

r? fmease @bors r+ rollup

@bors
Copy link
Collaborator

bors commented Aug 11, 2025

📌 Commit 6c571e7 has been approved by fmease

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 11, 2025
@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-19-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#![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;
---
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;
}



The actual fixed differed from the expected fixed
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args lint/unused/unused_attributes-must_use.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/lint/unused/unused_attributes-must_use.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/lint/unused/unused_attributes-must_use" "-A" "unused" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error: unused attribute `must_use`
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:60:1
   |
LL | #[must_use] //~ ERROR unused attribute
   | ^^^^^^^^^^^
   |
note: the built-in attribute `must_use` will be ignored, since it's applied to the macro invocation `global_asm`
  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:61:1
   |
LL | global_asm!("");
   | ^^^^^^^^^^
note: the lint level is defined here
  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:4:9
   |
LL | #![deny(unused_attributes, unused_must_use)]
   |         ^^^^^^^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to an extern crate
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:7:1
   |
LL | #[must_use] //~ ERROR `#[must_use]` has no effect
   | ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to a module
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:10:1
   |
LL | #[must_use] //~ ERROR `#[must_use]` has no effect
   | ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to a use
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:13:1
   |
LL | #[must_use] //~ ERROR `#[must_use]` has no effect
   | ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to a constant item
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:16:1
   |
LL | #[must_use] //~ ERROR `#[must_use]` has no effect
   | ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to a static item
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:18:1
   |
LL | #[must_use] //~ ERROR `#[must_use]` has no effect
   | ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to an inherent implementation block
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:35:1
   |
LL | #[must_use] //~ ERROR `#[must_use]` has no effect
   | ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to a foreign module
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:49:1
   |
LL | #[must_use] //~ ERROR `#[must_use]` has no effect
   | ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to a type alias
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:63:1
   |
LL | #[must_use] //~ ERROR `#[must_use]` has no effect
   | ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to a type parameter
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:66:8
   |
LL | fn qux<#[must_use] T>(_: T) {} //~ ERROR `#[must_use]` has no effect
   |        ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to an trait implementation block
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:81:1
   |
LL | #[must_use] //~ ERROR `#[must_use]` has no effect
   | ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to a trait alias
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:91:1
   |
LL | #[must_use] //~ ERROR `#[must_use]` has no effect
   | ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to a macro def
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:94:1
   |
LL | #[must_use] //~ ERROR `#[must_use]` has no effect
   | ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to a statement
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:102:5
   |
LL |     #[must_use] //~ ERROR `#[must_use]` has no effect
   |     ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to a closure
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:106:13
   |
LL |     let x = #[must_use] //~ ERROR `#[must_use]` has no effect
   |             ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to an match arm
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:128:9
   |
LL |         #[must_use] //~ ERROR `#[must_use]` has no effect
   |         ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to a struct field
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:136:28
   |
LL |     let s = PatternField { #[must_use]  foo: 123 }; //~ ERROR `#[must_use]` has no effect
   |                            ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to a pattern field
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:137:24
   |
LL |     let PatternField { #[must_use] foo } = s; //~ ERROR `#[must_use]` has no effect
   |                        ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to an associated const
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:70:5
   |
LL |     #[must_use] //~ ERROR `#[must_use]` has no effect
   |     ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to an associated type
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:72:5
   |
LL |     #[must_use] //~ ERROR `#[must_use]` has no effect
   |     ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to a provided trait method
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:85:5
   |
LL |     #[must_use] //~ ERROR `#[must_use]` has no effect
   |     ^^^^^^^^^^^

error: `#[must_use]` has no effect when applied to a foreign static item
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:52:5
   |
LL |     #[must_use] //~ ERROR `#[must_use]` has no effect
   |     ^^^^^^^^^^^

error: unused `X` that must be used
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:110:5
   |
LL |     X; //~ ERROR that must be used
   |     ^
   |
note: the lint level is defined here
  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:4:28
   |
LL | #![deny(unused_attributes, unused_must_use)]
   |                            ^^^^^^^^^^^^^^^
help: use `let _ = ...` to ignore the resulting value
   |
LL |     let _ = X; //~ ERROR that must be used
   |     +++++++

error: unused `Y` that must be used
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:111:5
   |
LL |     Y::Z; //~ ERROR that must be used
   |     ^^^^
   |
help: use `let _ = ...` to ignore the resulting value
   |
LL |     let _ = Y::Z; //~ ERROR that must be used
   |     +++++++

error: unused `U` that must be used
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:112:5
   |
LL |     U { unit: () }; //~ ERROR that must be used
   |     ^^^^^^^^^^^^^^
   |
help: use `let _ = ...` to ignore the resulting value
   |
LL |     let _ = U { unit: () }; //~ ERROR that must be used
   |     +++++++

error: unused return value of `U::method` that must be used
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:113:5
   |
LL |     U::method(); //~ ERROR that must be used
   |     ^^^^^^^^^^^
   |
help: use `let _ = ...` to ignore the resulting value
   |
LL |     let _ = U::method(); //~ ERROR that must be used
   |     +++++++

error: unused return value of `foo` that must be used
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:114:5
   |
---

error: unused return value of `foreign_foo` that must be used
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:117:9
   |
LL |         foreign_foo(); //~ ERROR that must be used
   |         ^^^^^^^^^^^^^
   |
help: use `let _ = ...` to ignore the resulting value
   |
LL |         let _ = foreign_foo(); //~ ERROR that must be used
   |         +++++++

error: unused return value of `Use::get_four` that must be used
##[error]  --> /checkout/tests/ui/lint/unused/unused_attributes-must_use.rs:125:5
   |
LL |     ().get_four(); //~ ERROR that must be used
   |     ^^^^^^^^^^^^^
   |
help: use `let _ = ...` to ignore the resulting value
   |
LL |     let _ = ().get_four(); //~ ERROR that must be used
   |     +++++++

error: aborting due to 29 previous errors
------------------------------------------

@fmease
Copy link
Member

fmease commented Aug 11, 2025

@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 11, 2025
@compiler-errors
Copy link
Member Author

Will bless when I am back at my computer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

unused must_use warning should have an auto-fix to just remove the useless attribute
6 participants