Skip to content

Commit

Permalink
Avoid linting for closures with inferred return types
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Nov 2, 2024
1 parent 6205bcf commit 139bb25
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
4 changes: 2 additions & 2 deletions clippy_lints/src/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/question_mark.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/question_mark.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,23 @@ 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);
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);
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;
Expand All @@ -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;
Expand Down

0 comments on commit 139bb25

Please sign in to comment.