Skip to content

Commit 8d9da4d

Browse files
committed
Auto merge of rust-lang#9276 - dswij:9164, r=flip1995
Ignore `match_like_matches_macro` when there is comment Closes rust-lang#9164 changelog: [`match_like_matches_macro`] is ignored when there is some comment inside the match block. Also add `span_contains_comment` util to check if given span contains comments.
2 parents 2d4d8e1 + b07d72b commit 8d9da4d

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-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();

clippy_utils/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ use rustc_hir::{
8787
Mutability, Node, Param, Pat, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, TraitItem, TraitItemKind,
8888
TraitRef, TyKind, UnOp,
8989
};
90+
use rustc_lexer::{tokenize, TokenKind};
9091
use rustc_lint::{LateContext, Level, Lint, LintContext};
9192
use rustc_middle::hir::place::PlaceBase;
9293
use rustc_middle::ty as rustc_ty;
@@ -104,6 +105,7 @@ use rustc_semver::RustcVersion;
104105
use rustc_session::Session;
105106
use rustc_span::hygiene::{ExpnKind, MacroKind};
106107
use rustc_span::source_map::original_sp;
108+
use rustc_span::source_map::SourceMap;
107109
use rustc_span::sym;
108110
use rustc_span::symbol::{kw, Symbol};
109111
use rustc_span::{Span, DUMMY_SP};
@@ -2278,6 +2280,18 @@ pub fn walk_to_expr_usage<'tcx, T>(
22782280
None
22792281
}
22802282

2283+
/// Checks whether a given span has any comment token
2284+
/// This checks for all types of comment: line "//", block "/**", doc "///" "//!"
2285+
pub fn span_contains_comment(sm: &SourceMap, span: Span) -> bool {
2286+
let Ok(snippet) = sm.span_to_snippet(span) else { return false };
2287+
return tokenize(&snippet).any(|token| {
2288+
matches!(
2289+
token.kind,
2290+
TokenKind::BlockComment { .. } | TokenKind::LineComment { .. }
2291+
)
2292+
});
2293+
}
2294+
22812295
macro_rules! op_utils {
22822296
($($name:ident $assign:ident)*) => {
22832297
/// Binary operation traits like `LangItem::Add`

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)