diff options
| author | GnomedDev <david2005thomas@gmail.com> | 2024-11-02 13:41:16 +0000 |
|---|---|---|
| committer | GnomedDev <david2005thomas@gmail.com> | 2024-11-02 13:41:16 +0000 |
| commit | 139bb25927f75c53e05d0c36dc9cfbc8081750c5 (patch) | |
| tree | 1e9618c997be0e7ed2c05ff45eb43d8f3fe41c52 | |
| parent | 6205bcf0f15038a74b9c156a541ea29479c50ffd (diff) | |
| download | rust-139bb25927f75c53e05d0c36dc9cfbc8081750c5.tar.gz rust-139bb25927f75c53e05d0c36dc9cfbc8081750c5.zip | |
Avoid linting for closures with inferred return types
| -rw-r--r-- | clippy_lints/src/question_mark.rs | 4 | ||||
| -rw-r--r-- | tests/ui/question_mark.fixed | 10 | ||||
| -rw-r--r-- | tests/ui/question_mark.rs | 10 | ||||
| -rw-r--r-- | tests/ui/question_mark.stderr | 8 |
4 files changed, 26 insertions, 6 deletions
diff --git a/clippy_lints/src/question_mark.rs b/clippy_lints/src/question_mark.rs index ce629717617..7d1ba45c3ea 100644 --- a/clippy_lints/src/question_mark.rs +++ b/clippy_lints/src/question_mark.rs @@ -16,7 +16,7 @@ use rustc_hir::LangItem::{self, OptionNone, OptionSome, ResultErr, ResultOk}; use rustc_hir::def::Res; use rustc_hir::{ Arm, BindingMode, Block, Body, ByRef, Expr, ExprKind, FnRetTy, HirId, LetStmt, MatchSource, Mutability, Node, Pat, - PatKind, PathSegment, QPath, Stmt, StmtKind, TyKind, + PatKind, PathSegment, QPath, Stmt, StmtKind, }; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::{self, Ty}; @@ -478,7 +478,7 @@ fn is_inferred_ret_closure(expr: &Expr<'_>) -> bool { }; match closure.fn_decl.output { - FnRetTy::Return(ret_ty) => matches!(ret_ty.kind, TyKind::Infer), + FnRetTy::Return(ret_ty) => ret_ty.is_suggestable_infer_ty(), FnRetTy::DefaultReturn(_) => true, } } diff --git a/tests/ui/question_mark.fixed b/tests/ui/question_mark.fixed index fb5bb953b72..b6e148e9f77 100644 --- a/tests/ui/question_mark.fixed +++ b/tests/ui/question_mark.fixed @@ -201,6 +201,16 @@ fn infer_check() { Ok(()) }; + + let closure = |x: Result<u8, ()>| -> Result<(), _> { + // `?` would fail here, as it expands to `Err(val.into())` which is not constrained. + let _val = match x { + Ok(val) => val, + Err(val) => return Err(val), + }; + + Ok(()) + }; } // see issue #8019 diff --git a/tests/ui/question_mark.rs b/tests/ui/question_mark.rs index f266bd9d5ad..48dc9eb0a62 100644 --- a/tests/ui/question_mark.rs +++ b/tests/ui/question_mark.rs @@ -248,6 +248,16 @@ fn infer_check() { Ok(()) }; + + let closure = |x: Result<u8, ()>| -> Result<(), _> { + // `?` would fail here, as it expands to `Err(val.into())` which is not constrained. + let _val = match x { + Ok(val) => val, + Err(val) => return Err(val), + }; + + Ok(()) + }; } // see issue #8019 diff --git a/tests/ui/question_mark.stderr b/tests/ui/question_mark.stderr index 8ba5f590669..0a48c4e80cb 100644 --- a/tests/ui/question_mark.stderr +++ b/tests/ui/question_mark.stderr @@ -163,7 +163,7 @@ LL | | }; | |_____^ help: try instead: `func_returning_result()?` error: this block may be rewritten with the `?` operator - --> tests/ui/question_mark.rs:274:5 + --> tests/ui/question_mark.rs:284:5 | LL | / if let Err(err) = func_returning_result() { LL | | return Err(err); @@ -171,7 +171,7 @@ LL | | } | |_____^ help: replace it with: `func_returning_result()?;` error: this block may be rewritten with the `?` operator - --> tests/ui/question_mark.rs:281:5 + --> tests/ui/question_mark.rs:291:5 | LL | / if let Err(err) = func_returning_result() { LL | | return Err(err); @@ -179,7 +179,7 @@ LL | | } | |_____^ help: replace it with: `func_returning_result()?;` error: this block may be rewritten with the `?` operator - --> tests/ui/question_mark.rs:358:13 + --> tests/ui/question_mark.rs:368:13 | LL | / if a.is_none() { LL | | return None; @@ -189,7 +189,7 @@ LL | | } | |_____________^ help: replace it with: `a?;` error: this `let...else` may be rewritten with the `?` operator - --> tests/ui/question_mark.rs:418:5 + --> tests/ui/question_mark.rs:428:5 | LL | / let Some(v) = bar.foo.owned.clone() else { LL | | return None; |
