Auto merge of #11286 - Centri3:#11283, r=Alexendoo

Suppress `question_mark` warning if `question_mark_used` is not allowed

Closes #11283

changelog: [`question_mark`]: Don't lint if `question_mark_used` is not allowed
This commit is contained in:
bors 2023-08-02 22:20:30 +00:00
commit 97d1cfa2b4
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::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::diagnostics::span_lint_and_sugg;
use clippy_utils::msrvs::Msrv; use clippy_utils::msrvs::Msrv;
use clippy_utils::source::snippet_with_applicability; use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::is_type_diagnostic_item; use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{ use clippy_utils::{
eq_expr_value, get_parent_node, higher, in_constant, is_else_clause, is_path_lang_item, is_res_lang_ctor, eq_expr_value, get_parent_node, higher, in_constant, is_else_clause, is_lint_allowed, is_path_lang_item,
pat_and_expr_can_be_question_mark, path_to_local, path_to_local_id, peel_blocks, peel_blocks_with_stmt, 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 if_chain::if_chain;
use rustc_errors::Applicability; 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 { impl<'tcx> LateLintPass<'tcx> for QuestionMark {
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) { 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) { if !in_constant(cx, stmt.hir_id) {
check_let_some_else_return_none(cx, stmt); check_let_some_else_return_none(cx, stmt);
} }
self.check_manual_let_else(cx, stmt); self.check_manual_let_else(cx, stmt);
} }
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { 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_is_none_or_err_and_early_return(cx, expr);
self.check_if_let_some_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 // no warning
let _ = if let Err(e) = x { Err(e) } else { Ok(0) }; 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) Ok(y)
} }

View file

@ -170,6 +170,23 @@ fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
// no warning // no warning
let _ = if let Err(e) = x { Err(e) } else { Ok(0) }; 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) Ok(y)
} }

View file

@ -115,7 +115,7 @@ LL | | }
| |_____^ help: replace it with: `x?;` | |_____^ help: replace it with: `x?;`
error: this block may be rewritten with the `?` operator 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 | / if let Err(err) = func_returning_result() {
LL | | return Err(err); LL | | return Err(err);
@ -123,7 +123,7 @@ LL | | }
| |_____^ help: replace it with: `func_returning_result()?;` | |_____^ help: replace it with: `func_returning_result()?;`
error: this block may be rewritten with the `?` operator 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 | / if let Err(err) = func_returning_result() {
LL | | return Err(err); LL | | return Err(err);
@ -131,7 +131,7 @@ LL | | }
| |_____^ help: replace it with: `func_returning_result()?;` | |_____^ help: replace it with: `func_returning_result()?;`
error: this block may be rewritten with the `?` operator 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 | / if a.is_none() {
LL | | return None; LL | | return None;