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

Commit b07d72b

Browse files
committed
Ignore when there is comment
1 parent 51e9113 commit b07d72b

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

clippy_lints/src/matches/match_like_matches.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::is_wild;
33
use clippy_utils::source::snippet_with_applicability;
4+
use clippy_utils::span_contains_comment;
45
use rustc_ast::{Attribute, LitKind};
56
use rustc_errors::Applicability;
67
use rustc_hir::{Arm, BorrowKind, Expr, ExprKind, Guard, Pat};
7-
use rustc_lint::LateContext;
8+
use rustc_lint::{LateContext, LintContext};
89
use rustc_middle::ty;
910
use rustc_span::source_map::Spanned;
1011

@@ -76,6 +77,7 @@ where
7677
>,
7778
{
7879
if_chain! {
80+
if !span_contains_comment(cx.sess().source_map(), expr.span);
7981
if iter.len() >= 2;
8082
if cx.typeck_results().expr_ty(expr).is_bool();
8183
if let Some((_, last_pat_opt, last_expr, _)) = iter.next_back();

tests/ui/match_expr_like_matches_macro.fixed

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,29 @@ fn main() {
167167
_ => false,
168168
};
169169
}
170+
171+
let x = ' ';
172+
// ignore if match block contains comment
173+
let _line_comments = match x {
174+
// numbers are bad!
175+
'1' | '2' | '3' => true,
176+
// spaces are very important to be true.
177+
' ' => true,
178+
// as are dots
179+
'.' => true,
180+
_ => false,
181+
};
182+
183+
let _block_comments = match x {
184+
/* numbers are bad!
185+
*/
186+
'1' | '2' | '3' => true,
187+
/* spaces are very important to be true.
188+
*/
189+
' ' => true,
190+
/* as are dots
191+
*/
192+
'.' => true,
193+
_ => false,
194+
};
170195
}

tests/ui/match_expr_like_matches_macro.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,29 @@ fn main() {
208208
_ => false,
209209
};
210210
}
211+
212+
let x = ' ';
213+
// ignore if match block contains comment
214+
let _line_comments = match x {
215+
// numbers are bad!
216+
'1' | '2' | '3' => true,
217+
// spaces are very important to be true.
218+
' ' => true,
219+
// as are dots
220+
'.' => true,
221+
_ => false,
222+
};
223+
224+
let _block_comments = match x {
225+
/* numbers are bad!
226+
*/
227+
'1' | '2' | '3' => true,
228+
/* spaces are very important to be true.
229+
*/
230+
' ' => true,
231+
/* as are dots
232+
*/
233+
'.' => true,
234+
_ => false,
235+
};
211236
}

0 commit comments

Comments
 (0)