respect comments in question_mark

This commit is contained in:
y21 2023-12-23 16:21:58 +01:00
parent dfb4ff8bbf
commit e44caea259
3 changed files with 20 additions and 3 deletions

View File

@ -8,7 +8,7 @@
use clippy_utils::{ use clippy_utils::{
eq_expr_value, get_parent_node, higher, in_constant, is_else_clause, is_lint_allowed, is_path_lang_item, 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, is_res_lang_ctor, pat_and_expr_can_be_question_mark, path_to_local, path_to_local_id, peel_blocks,
peel_blocks_with_stmt, peel_blocks_with_stmt, span_contains_comment,
}; };
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::def::Res; use rustc_hir::def::Res;
@ -121,8 +121,9 @@ fn check_let_some_else_return_none(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
els: Some(els), els: Some(els),
.. ..
}) = stmt.kind }) = stmt.kind
&& let Some(els) = find_let_else_ret_expression(els) && let Some(ret) = find_let_else_ret_expression(els)
&& let Some(inner_pat) = pat_and_expr_can_be_question_mark(cx, pat, els) && let Some(inner_pat) = pat_and_expr_can_be_question_mark(cx, pat, ret)
&& !span_contains_comment(cx.tcx.sess.source_map(), els.span)
{ {
let mut applicability = Applicability::MaybeIncorrect; let mut applicability = Applicability::MaybeIncorrect;
let init_expr_str = snippet_with_applicability(cx, init_expr.span, "..", &mut applicability); let init_expr_str = snippet_with_applicability(cx, init_expr.span, "..", &mut applicability);

View File

@ -71,5 +71,13 @@ fn issue11993(y: Option<i32>) -> Option<i32> {
return None; return None;
}; };
let Some(x) = y else {
// Roses are red,
// violets are blue,
// please keep this comment,
// it's art, you know?
return None;
};
None None
} }

View File

@ -78,5 +78,13 @@ fn issue11993(y: Option<i32>) -> Option<i32> {
return None; return None;
}; };
let Some(x) = y else {
// Roses are red,
// violets are blue,
// please keep this comment,
// it's art, you know?
return None;
};
None None
} }