Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit e44caea

Browse files
committed
respect comments in question_mark
1 parent dfb4ff8 commit e44caea

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

clippy_lints/src/question_mark.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use clippy_utils::ty::is_type_diagnostic_item;
88
use clippy_utils::{
99
eq_expr_value, get_parent_node, higher, in_constant, is_else_clause, is_lint_allowed, is_path_lang_item,
1010
is_res_lang_ctor, pat_and_expr_can_be_question_mark, path_to_local, path_to_local_id, peel_blocks,
11-
peel_blocks_with_stmt,
11+
peel_blocks_with_stmt, span_contains_comment,
1212
};
1313
use rustc_errors::Applicability;
1414
use rustc_hir::def::Res;
@@ -121,8 +121,9 @@ fn check_let_some_else_return_none(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
121121
els: Some(els),
122122
..
123123
}) = stmt.kind
124-
&& let Some(els) = find_let_else_ret_expression(els)
125-
&& let Some(inner_pat) = pat_and_expr_can_be_question_mark(cx, pat, els)
124+
&& let Some(ret) = find_let_else_ret_expression(els)
125+
&& let Some(inner_pat) = pat_and_expr_can_be_question_mark(cx, pat, ret)
126+
&& !span_contains_comment(cx.tcx.sess.source_map(), els.span)
126127
{
127128
let mut applicability = Applicability::MaybeIncorrect;
128129
let init_expr_str = snippet_with_applicability(cx, init_expr.span, "..", &mut applicability);

tests/ui/manual_let_else_question_mark.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,13 @@ fn issue11993(y: Option<i32>) -> Option<i32> {
7171
return None;
7272
};
7373

74+
let Some(x) = y else {
75+
// Roses are red,
76+
// violets are blue,
77+
// please keep this comment,
78+
// it's art, you know?
79+
return None;
80+
};
81+
7482
None
7583
}

tests/ui/manual_let_else_question_mark.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,13 @@ fn issue11993(y: Option<i32>) -> Option<i32> {
7878
return None;
7979
};
8080

81+
let Some(x) = y else {
82+
// Roses are red,
83+
// violets are blue,
84+
// please keep this comment,
85+
// it's art, you know?
86+
return None;
87+
};
88+
8189
None
8290
}

0 commit comments

Comments
 (0)