diff options
| author | blyxyas <blyxyas@gmail.com> | 2023-04-03 16:01:30 +0200 |
|---|---|---|
| committer | blyxyas <blyxyas@gmail.com> | 2023-04-03 16:07:17 +0200 |
| commit | a37eb4dfc97cdcbd177ccc0fda1909014b145635 (patch) | |
| tree | 6c69ef5482a2f4603ed02c1cf8802dca48717287 | |
| parent | d60274355894c0e0768b034ab538aa0625fc2a91 (diff) | |
| download | rust-a37eb4dfc97cdcbd177ccc0fda1909014b145635.tar.gz rust-a37eb4dfc97cdcbd177ccc0fda1909014b145635.zip | |
Fix false negative on `Result<(), ()>`
| -rw-r--r-- | clippy_lints/src/functions/must_use.rs | 12 | ||||
| -rw-r--r-- | tests/ui/double_must_use.stderr | 10 |
2 files changed, 20 insertions, 2 deletions
diff --git a/clippy_lints/src/functions/must_use.rs b/clippy_lints/src/functions/must_use.rs index ab68d7a3726..d0ad2628264 100644 --- a/clippy_lints/src/functions/must_use.rs +++ b/clippy_lints/src/functions/must_use.rs @@ -3,6 +3,7 @@ use rustc_ast::ast::Attribute; use rustc_errors::Applicability; use rustc_hir::def_id::DefIdSet; use rustc_hir::{self as hir, def::Res, QPath}; +use rustc_infer::infer::TyCtxtInferExt; use rustc_lint::{LateContext, LintContext}; use rustc_middle::{ lint::in_external_macro, @@ -113,7 +114,16 @@ fn check_needless_must_use( diag.span_suggestion(attr.span, "remove the attribute", "", Applicability::MachineApplicable); }, ); - } else if attr.value_str().is_none() && is_must_use_ty(cx, return_ty(cx, item_id)) && !sig.header.is_async() { + } else if attr.value_str().is_none() && is_must_use_ty(cx, return_ty(cx, item_id)) { + // Ignore async functions unless Future::Output type is a must_use type + if sig.header.is_async() { + let infcx = cx.tcx.infer_ctxt().build(); + if let Some(future_ty) = infcx.get_impl_future_output_ty(return_ty(cx, item_id)) + && !is_must_use_ty(cx, future_ty) { + return; + } + } + span_lint_and_help( cx, DOUBLE_MUST_USE, diff --git a/tests/ui/double_must_use.stderr b/tests/ui/double_must_use.stderr index 3d34557a881..49ab2ea3e12 100644 --- a/tests/ui/double_must_use.stderr +++ b/tests/ui/double_must_use.stderr @@ -23,5 +23,13 @@ LL | pub fn must_use_array() -> [Result<(), ()>; 1] { | = help: either add some descriptive text or remove the attribute -error: aborting due to 3 previous errors +error: this function has an empty `#[must_use]` attribute, but returns a type already marked as `#[must_use]` + --> $DIR/double_must_use.rs:31:1 + | +LL | async fn async_must_use_result() -> Result<(), ()> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: either add some descriptive text or remove the attribute + +error: aborting due to 4 previous errors |
