Ignore when there is comment

This commit is contained in:
dswij 2022-08-28 00:06:30 +08:00
parent 51e9113c60
commit b07d72b69e
3 changed files with 53 additions and 1 deletions

View File

@ -1,10 +1,11 @@
use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_wild; use clippy_utils::is_wild;
use clippy_utils::source::snippet_with_applicability; use clippy_utils::source::snippet_with_applicability;
use clippy_utils::span_contains_comment;
use rustc_ast::{Attribute, LitKind}; use rustc_ast::{Attribute, LitKind};
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::{Arm, BorrowKind, Expr, ExprKind, Guard, Pat}; use rustc_hir::{Arm, BorrowKind, Expr, ExprKind, Guard, Pat};
use rustc_lint::LateContext; use rustc_lint::{LateContext, LintContext};
use rustc_middle::ty; use rustc_middle::ty;
use rustc_span::source_map::Spanned; use rustc_span::source_map::Spanned;
@ -76,6 +77,7 @@ where
>, >,
{ {
if_chain! { if_chain! {
if !span_contains_comment(cx.sess().source_map(), expr.span);
if iter.len() >= 2; if iter.len() >= 2;
if cx.typeck_results().expr_ty(expr).is_bool(); if cx.typeck_results().expr_ty(expr).is_bool();
if let Some((_, last_pat_opt, last_expr, _)) = iter.next_back(); if let Some((_, last_pat_opt, last_expr, _)) = iter.next_back();

View File

@ -167,4 +167,29 @@ fn main() {
_ => false, _ => false,
}; };
} }
let x = ' ';
// ignore if match block contains comment
let _line_comments = match x {
// numbers are bad!
'1' | '2' | '3' => true,
// spaces are very important to be true.
' ' => true,
// as are dots
'.' => true,
_ => false,
};
let _block_comments = match x {
/* numbers are bad!
*/
'1' | '2' | '3' => true,
/* spaces are very important to be true.
*/
' ' => true,
/* as are dots
*/
'.' => true,
_ => false,
};
} }

View File

@ -208,4 +208,29 @@ fn main() {
_ => false, _ => false,
}; };
} }
let x = ' ';
// ignore if match block contains comment
let _line_comments = match x {
// numbers are bad!
'1' | '2' | '3' => true,
// spaces are very important to be true.
' ' => true,
// as are dots
'.' => true,
_ => false,
};
let _block_comments = match x {
/* numbers are bad!
*/
'1' | '2' | '3' => true,
/* spaces are very important to be true.
*/
' ' => true,
/* as are dots
*/
'.' => true,
_ => false,
};
} }