Skip to content

Commit 139bb25

Browse files
committed
Avoid linting for closures with inferred return types
1 parent 6205bcf commit 139bb25

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

clippy_lints/src/question_mark.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_hir::LangItem::{self, OptionNone, OptionSome, ResultErr, ResultOk};
1616
use rustc_hir::def::Res;
1717
use rustc_hir::{
1818
Arm, BindingMode, Block, Body, ByRef, Expr, ExprKind, FnRetTy, HirId, LetStmt, MatchSource, Mutability, Node, Pat,
19-
PatKind, PathSegment, QPath, Stmt, StmtKind, TyKind,
19+
PatKind, PathSegment, QPath, Stmt, StmtKind,
2020
};
2121
use rustc_lint::{LateContext, LateLintPass};
2222
use rustc_middle::ty::{self, Ty};
@@ -478,7 +478,7 @@ fn is_inferred_ret_closure(expr: &Expr<'_>) -> bool {
478478
};
479479

480480
match closure.fn_decl.output {
481-
FnRetTy::Return(ret_ty) => matches!(ret_ty.kind, TyKind::Infer),
481+
FnRetTy::Return(ret_ty) => ret_ty.is_suggestable_infer_ty(),
482482
FnRetTy::DefaultReturn(_) => true,
483483
}
484484
}

tests/ui/question_mark.fixed

+10
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,16 @@ fn infer_check() {
201201

202202
Ok(())
203203
};
204+
205+
let closure = |x: Result<u8, ()>| -> Result<(), _> {
206+
// `?` would fail here, as it expands to `Err(val.into())` which is not constrained.
207+
let _val = match x {
208+
Ok(val) => val,
209+
Err(val) => return Err(val),
210+
};
211+
212+
Ok(())
213+
};
204214
}
205215

206216
// see issue #8019

tests/ui/question_mark.rs

+10
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,16 @@ fn infer_check() {
248248

249249
Ok(())
250250
};
251+
252+
let closure = |x: Result<u8, ()>| -> Result<(), _> {
253+
// `?` would fail here, as it expands to `Err(val.into())` which is not constrained.
254+
let _val = match x {
255+
Ok(val) => val,
256+
Err(val) => return Err(val),
257+
};
258+
259+
Ok(())
260+
};
251261
}
252262

253263
// see issue #8019

tests/ui/question_mark.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -163,23 +163,23 @@ LL | | };
163163
| |_____^ help: try instead: `func_returning_result()?`
164164

165165
error: this block may be rewritten with the `?` operator
166-
--> tests/ui/question_mark.rs:274:5
166+
--> tests/ui/question_mark.rs:284:5
167167
|
168168
LL | / if let Err(err) = func_returning_result() {
169169
LL | | return Err(err);
170170
LL | | }
171171
| |_____^ help: replace it with: `func_returning_result()?;`
172172

173173
error: this block may be rewritten with the `?` operator
174-
--> tests/ui/question_mark.rs:281:5
174+
--> tests/ui/question_mark.rs:291:5
175175
|
176176
LL | / if let Err(err) = func_returning_result() {
177177
LL | | return Err(err);
178178
LL | | }
179179
| |_____^ help: replace it with: `func_returning_result()?;`
180180

181181
error: this block may be rewritten with the `?` operator
182-
--> tests/ui/question_mark.rs:358:13
182+
--> tests/ui/question_mark.rs:368:13
183183
|
184184
LL | / if a.is_none() {
185185
LL | | return None;
@@ -189,7 +189,7 @@ LL | | }
189189
| |_____________^ help: replace it with: `a?;`
190190

191191
error: this `let...else` may be rewritten with the `?` operator
192-
--> tests/ui/question_mark.rs:418:5
192+
--> tests/ui/question_mark.rs:428:5
193193
|
194194
LL | / let Some(v) = bar.foo.owned.clone() else {
195195
LL | | return None;

0 commit comments

Comments
 (0)