Skip to content

Commit 4d49065

Browse files
committed
Suppress question_mark if question_mark_used is not allowed
1 parent 78f5e0d commit 4d49065

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

clippy_lints/src/question_mark.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use crate::manual_let_else::{MatchLintBehaviour, MANUAL_LET_ELSE};
2+
use crate::question_mark_used::QUESTION_MARK_USED;
23
use clippy_utils::diagnostics::span_lint_and_sugg;
34
use clippy_utils::msrvs::Msrv;
45
use clippy_utils::source::snippet_with_applicability;
56
use clippy_utils::ty::is_type_diagnostic_item;
67
use clippy_utils::{
7-
eq_expr_value, get_parent_node, higher, in_constant, is_else_clause, is_path_lang_item, is_res_lang_ctor,
8-
pat_and_expr_can_be_question_mark, path_to_local, path_to_local_id, peel_blocks, peel_blocks_with_stmt,
8+
eq_expr_value, get_parent_node, higher, in_constant, is_else_clause, is_lint_allowed, is_path_lang_item,
9+
is_res_lang_ctor, pat_and_expr_can_be_question_mark, path_to_local, path_to_local_id, peel_blocks,
10+
peel_blocks_with_stmt,
911
};
1012
use if_chain::if_chain;
1113
use rustc_errors::Applicability;
@@ -299,13 +301,17 @@ fn is_try_block(cx: &LateContext<'_>, bl: &rustc_hir::Block<'_>) -> bool {
299301

300302
impl<'tcx> LateLintPass<'tcx> for QuestionMark {
301303
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
304+
if !is_lint_allowed(cx, QUESTION_MARK_USED, stmt.hir_id) {
305+
return;
306+
}
307+
302308
if !in_constant(cx, stmt.hir_id) {
303309
check_let_some_else_return_none(cx, stmt);
304310
}
305311
self.check_manual_let_else(cx, stmt);
306312
}
307313
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
308-
if !in_constant(cx, expr.hir_id) {
314+
if !in_constant(cx, expr.hir_id) && is_lint_allowed(cx, QUESTION_MARK_USED, expr.hir_id) {
309315
self.check_is_none_or_err_and_early_return(cx, expr);
310316
self.check_if_let_some_or_err_and_early_return(cx, expr);
311317
}

tests/ui/question_mark.fixed

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,23 @@ fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
138138
// no warning
139139
let _ = if let Err(e) = x { Err(e) } else { Ok(0) };
140140

141+
// issue #11283
142+
// no warning
143+
#[warn(clippy::question_mark_used)]
144+
{
145+
if let Err(err) = Ok(()) {
146+
return Err(err);
147+
}
148+
149+
if Err::<i32, _>(0).is_err() {
150+
return Err(0);
151+
} else {
152+
return Ok(0);
153+
}
154+
155+
unreachable!()
156+
}
157+
141158
Ok(y)
142159
}
143160

tests/ui/question_mark.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,23 @@ fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
170170
// no warning
171171
let _ = if let Err(e) = x { Err(e) } else { Ok(0) };
172172

173+
// issue #11283
174+
// no warning
175+
#[warn(clippy::question_mark_used)]
176+
{
177+
if let Err(err) = Ok(()) {
178+
return Err(err);
179+
}
180+
181+
if Err::<i32, _>(0).is_err() {
182+
return Err(0);
183+
} else {
184+
return Ok(0);
185+
}
186+
187+
unreachable!()
188+
}
189+
173190
Ok(y)
174191
}
175192

tests/ui/question_mark.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,23 @@ LL | | }
115115
| |_____^ help: replace it with: `x?;`
116116

117117
error: this block may be rewritten with the `?` operator
118-
--> $DIR/question_mark.rs:197:5
118+
--> $DIR/question_mark.rs:214:5
119119
|
120120
LL | / if let Err(err) = func_returning_result() {
121121
LL | | return Err(err);
122122
LL | | }
123123
| |_____^ help: replace it with: `func_returning_result()?;`
124124

125125
error: this block may be rewritten with the `?` operator
126-
--> $DIR/question_mark.rs:204:5
126+
--> $DIR/question_mark.rs:221:5
127127
|
128128
LL | / if let Err(err) = func_returning_result() {
129129
LL | | return Err(err);
130130
LL | | }
131131
| |_____^ help: replace it with: `func_returning_result()?;`
132132

133133
error: this block may be rewritten with the `?` operator
134-
--> $DIR/question_mark.rs:281:13
134+
--> $DIR/question_mark.rs:298:13
135135
|
136136
LL | / if a.is_none() {
137137
LL | | return None;

0 commit comments

Comments
 (0)