Suppress question_mark if question_mark_used is not allowed

This commit is contained in:
Catherine Flores 2023-08-02 14:13:16 -05:00
parent 78f5e0d3ec
commit 4d49065a6c
4 changed files with 46 additions and 6 deletions

View File

@ -1,11 +1,13 @@
use crate::manual_let_else::{MatchLintBehaviour, MANUAL_LET_ELSE};
use crate::question_mark_used::QUESTION_MARK_USED;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::msrvs::Msrv;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{
eq_expr_value, get_parent_node, higher, in_constant, is_else_clause, is_path_lang_item, is_res_lang_ctor,
pat_and_expr_can_be_question_mark, path_to_local, path_to_local_id, peel_blocks, peel_blocks_with_stmt,
eq_expr_value, get_parent_node, higher, in_constant, is_else_clause, is_lint_allowed, is_path_lang_item,
is_res_lang_ctor, pat_and_expr_can_be_question_mark, path_to_local, path_to_local_id, peel_blocks,
peel_blocks_with_stmt,
};
use if_chain::if_chain;
use rustc_errors::Applicability;
@ -299,13 +301,17 @@ fn is_try_block(cx: &LateContext<'_>, bl: &rustc_hir::Block<'_>) -> bool {
impl<'tcx> LateLintPass<'tcx> for QuestionMark {
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
if !is_lint_allowed(cx, QUESTION_MARK_USED, stmt.hir_id) {
return;
}
if !in_constant(cx, stmt.hir_id) {
check_let_some_else_return_none(cx, stmt);
}
self.check_manual_let_else(cx, stmt);
}
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if !in_constant(cx, expr.hir_id) {
if !in_constant(cx, expr.hir_id) && is_lint_allowed(cx, QUESTION_MARK_USED, expr.hir_id) {
self.check_is_none_or_err_and_early_return(cx, expr);
self.check_if_let_some_or_err_and_early_return(cx, expr);
}

View File

@ -138,6 +138,23 @@ fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
// no warning
let _ = if let Err(e) = x { Err(e) } else { Ok(0) };
// issue #11283
// no warning
#[warn(clippy::question_mark_used)]
{
if let Err(err) = Ok(()) {
return Err(err);
}
if Err::<i32, _>(0).is_err() {
return Err(0);
} else {
return Ok(0);
}
unreachable!()
}
Ok(y)
}

View File

@ -170,6 +170,23 @@ fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
// no warning
let _ = if let Err(e) = x { Err(e) } else { Ok(0) };
// issue #11283
// no warning
#[warn(clippy::question_mark_used)]
{
if let Err(err) = Ok(()) {
return Err(err);
}
if Err::<i32, _>(0).is_err() {
return Err(0);
} else {
return Ok(0);
}
unreachable!()
}
Ok(y)
}

View File

@ -115,7 +115,7 @@ LL | | }
| |_____^ help: replace it with: `x?;`
error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:197:5
--> $DIR/question_mark.rs:214:5
|
LL | / if let Err(err) = func_returning_result() {
LL | | return Err(err);
@ -123,7 +123,7 @@ LL | | }
| |_____^ help: replace it with: `func_returning_result()?;`
error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:204:5
--> $DIR/question_mark.rs:221:5
|
LL | / if let Err(err) = func_returning_result() {
LL | | return Err(err);
@ -131,7 +131,7 @@ LL | | }
| |_____^ help: replace it with: `func_returning_result()?;`
error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:281:13
--> $DIR/question_mark.rs:298:13
|
LL | / if a.is_none() {
LL | | return None;