Skip to content
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

Lint against more useless #[must_use] attributes #93926

Merged
merged 1 commit into from
Mar 1, 2022
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
1 change: 0 additions & 1 deletion compiler/rustc_macros/src/session_diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,6 @@ impl<'a> SessionDiagnosticDeriveBuilder<'a> {
// next call to `it.next()` retrieves the next character.
while let Some(c) = it.next() {
if c == '{' && *it.peek().unwrap_or(&'\0') != '{' {
#[must_use]
let mut eat_argument = || -> Option<String> {
let mut result = String::new();
// Format specifiers look like
Expand Down
35 changes: 34 additions & 1 deletion compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ impl CheckAttrVisitor<'_> {
}

/// Warns against some misuses of `#[must_use]`
fn check_must_use(&self, hir_id: HirId, attr: &Attribute, span: Span, _target: Target) -> bool {
fn check_must_use(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) -> bool {
let node = self.tcx.hir().get(hir_id);
if let Some(fn_node) = node.fn_kind() {
if let rustc_hir::IsAsync::Async = fn_node.asyncness() {
Expand All @@ -1131,6 +1131,39 @@ impl CheckAttrVisitor<'_> {
}
}

if !matches!(
target,
Target::Fn
| Target::Enum
| Target::Struct
| Target::Union
| Target::Method(_)
| Target::ForeignFn
// `impl Trait` in return position can trip
// `unused_must_use` if `Trait` is marked as
// `#[must_use]`
| Target::Trait
) {
let article = match target {
Target::ExternCrate
| Target::OpaqueTy
| Target::Enum
| Target::Impl
| Target::Expression
| Target::Arm
| Target::AssocConst
| Target::AssocTy => "an",
_ => "a",
};

self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
lint.build(&format!(
"`#[must_use]` has no effect when applied to {article} {target}"
))
.emit();
});
}

// For now, its always valid
true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
//~^^ WARN this was previously accepted by the compiler
// see issue-43106-gating-of-rustc_deprecated.rs
#![must_use]
//~^ WARN `#[must_use]` has no effect
// see issue-43106-gating-of-stable.rs
// see issue-43106-gating-of-unstable.rs
// see issue-43106-gating-of-deprecated.rs
Expand Down Expand Up @@ -597,17 +598,17 @@ mod deprecated {
#[deprecated] impl super::StructForDeprecated { }
}

#[must_use]
#[must_use] //~ WARN `#[must_use]` has no effect
mod must_use {
mod inner { #![must_use] }
mod inner { #![must_use] } //~ WARN `#[must_use]` has no effect

#[must_use] fn f() { }

#[must_use] struct S;

#[must_use] type T = S;
#[must_use] type T = S; //~ WARN `#[must_use]` has no effect

#[must_use] impl S { }
#[must_use] impl S { } //~ WARN `#[must_use]` has no effect
}

#[windows_subsystem = "windows"]
Expand Down
Loading