Avoid linting for closures with inferred return types

This commit is contained in:
GnomedDev 2024-11-02 13:41:16 +00:00
parent 6205bcf0f1
commit 139bb25927
4 changed files with 26 additions and 6 deletions

View file

@ -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,
}
}

View file

@ -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

View file

@ -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

View file

@ -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;