Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions clippy_dev/src/new_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,18 +526,14 @@ fn parse_mod_file(path: &Path, contents: &str) -> (&'static str, usize) {
let mut captures = [Capture::EMPTY];
while let Some(name) = cursor.find_any_ident() {
match cursor.get_text(name) {
"declare_clippy_lint" => {
if cursor.match_all(&[Bang, OpenBrace], &mut []) && cursor.find_pat(CloseBrace) {
decl_end = Some(cursor.pos());
}
"declare_clippy_lint" if cursor.match_all(&[Bang, OpenBrace], &mut []) && cursor.find_pat(CloseBrace) => {
decl_end = Some(cursor.pos());
},
"impl" => {
if cursor.match_all(&[Lt, Lifetime, Gt, CaptureIdent], &mut captures) {
match cursor.get_text(captures[0]) {
"LateLintPass" => context = Some("LateContext"),
"EarlyLintPass" => context = Some("EarlyContext"),
_ => {},
}
"impl" if cursor.match_all(&[Lt, Lifetime, Gt, CaptureIdent], &mut captures) => {
match cursor.get_text(captures[0]) {
"LateLintPass" => context = Some("LateContext"),
"EarlyLintPass" => context = Some("EarlyContext"),
_ => {},
}
},
_ => {},
Expand Down
6 changes: 2 additions & 4 deletions clippy_lints/src/casts/unnecessary_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ pub(super) fn check<'tcx>(
// Ignore casts to pointers that are aliases or cfg dependant, e.g.
// - p as *const std::ffi::c_char (alias)
// - p as *const std::os::raw::c_char (cfg dependant)
TyKind::Path(qpath) => {
if is_ty_alias(&qpath) || is_hir_ty_cfg_dependant(cx, to_pointee.ty) {
return false;
}
TyKind::Path(qpath) if is_ty_alias(&qpath) || is_hir_ty_cfg_dependant(cx, to_pointee.ty) => {
return false;
},
// Ignore `p as *const _`
TyKind::Infer(()) => return false,
Expand Down
6 changes: 2 additions & 4 deletions clippy_lints/src/cognitive_complexity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ impl CognitiveComplexity {
}
cc += arms.iter().filter(|arm| arm.guard.is_some()).count() as u64;
},
ExprKind::Ret(_) => {
if !matches!(prev_expr, Some(ExprKind::Ret(_))) {
returns += 1;
}
ExprKind::Ret(_) if !matches!(prev_expr, Some(ExprKind::Ret(_))) => {
returns += 1;
},
_ => {},
}
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/collapsible_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ fn expr_block<'tcx>(block: &Block<'tcx>) -> Option<&'tcx Expr<'tcx>> {
}

/// If the expression is a `||`, suggest parentheses around it.
fn parens_around(expr: &Expr<'_>) -> Vec<(Span, String)> {
pub(super) fn parens_around(expr: &Expr<'_>) -> Vec<(Span, String)> {
if let ExprKind::Binary(op, _, _) = expr.peel_drop_temps().kind
&& op.node == BinOpKind::Or
{
Expand All @@ -334,7 +334,7 @@ fn span_extract_keyword(sm: &SourceMap, span: Span, keyword: &str) -> Option<Spa
}

/// Peel the parentheses from an `if` expression, e.g. `((if true {} else {}))`.
fn peel_parens(sm: &SourceMap, mut span: Span) -> (Span, Span, Span) {
pub(super) fn peel_parens(sm: &SourceMap, mut span: Span) -> (Span, Span, Span) {
use crate::rustc_span::Pos;

let start = span.shrink_to_lo();
Expand Down
16 changes: 4 additions & 12 deletions clippy_lints/src/doc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,19 +763,11 @@ impl<'tcx> LateLintPass<'tcx> for Documentation {
self.check_private_items,
);
match item.kind {
ItemKind::Fn { sig, body, .. } => {
ItemKind::Fn { sig, body, .. }
if !(is_entrypoint_fn(cx, item.owner_id.to_def_id())
|| item.span.in_external_macro(cx.tcx.sess.source_map()))
{
missing_headers::check(
cx,
item.owner_id,
sig,
headers,
Some(body),
self.check_private_items,
);
}
|| item.span.in_external_macro(cx.tcx.sess.source_map())) =>
{
missing_headers::check(cx, item.owner_id, sig, headers, Some(body), self.check_private_items);
},
ItemKind::Trait(_, _, unsafety, ..) => match (headers.safety, unsafety) {
(false, Safety::Unsafe) => span_lint(
Expand Down
74 changes: 36 additions & 38 deletions clippy_lints/src/double_parens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,28 @@ impl EarlyLintPass for DoubleParens {
// ((..))
// ^^^^^^ expr
// ^^^^ inner
ExprKind::Paren(inner) if matches!(inner.kind, ExprKind::Paren(_) | ExprKind::Tup(_)) => {
if expr.span.eq_ctxt(inner.span)
ExprKind::Paren(inner)
if matches!(inner.kind, ExprKind::Paren(_) | ExprKind::Tup(_))
&& expr.span.eq_ctxt(inner.span)
&& !expr.span.in_external_macro(cx.sess().source_map())
&& check_source(cx, inner)
{
// suggest removing the outer parens
&& check_source(cx, inner) =>
{
// suggest removing the outer parens

let mut applicability = Applicability::MachineApplicable;
// We don't need to use `snippet_with_context` here, because:
// - if `inner`'s `ctxt` is from macro, we don't lint in the first place (see the check above)
// - otherwise, calling `snippet_with_applicability` on a not-from-macro span is fine
let sugg = snippet_with_applicability(cx.sess(), inner.span, "_", &mut applicability);
span_lint_and_sugg(
cx,
DOUBLE_PARENS,
expr.span,
"unnecessary parentheses",
"remove them",
sugg.to_string(),
applicability,
);
}
let mut applicability = Applicability::MachineApplicable;
// We don't need to use `snippet_with_context` here, because:
// - if `inner`'s `ctxt` is from macro, we don't lint in the first place (see the check above)
// - otherwise, calling `snippet_with_applicability` on a not-from-macro span is fine
let sugg = snippet_with_applicability(cx.sess(), inner.span, "_", &mut applicability);
span_lint_and_sugg(
cx,
DOUBLE_PARENS,
expr.span,
"unnecessary parentheses",
"remove them",
sugg.to_string(),
applicability,
);
},

// func((n))
Expand All @@ -76,26 +76,24 @@ impl EarlyLintPass for DoubleParens {
// ^ inner
ExprKind::Call(_, args) | ExprKind::MethodCall(box MethodCall { args, .. })
if let [arg] = &**args
&& let ExprKind::Paren(inner) = &arg.kind =>
{
if expr.span.eq_ctxt(arg.span)
&& let ExprKind::Paren(inner) = &arg.kind
&& expr.span.eq_ctxt(arg.span)
&& !arg.span.in_external_macro(cx.sess().source_map())
&& check_source(cx, arg)
{
// suggest removing the inner parens
&& check_source(cx, arg) =>
{
// suggest removing the inner parens

let mut applicability = Applicability::MachineApplicable;
let sugg = snippet_with_context(cx.sess(), inner.span, arg.span.ctxt(), "_", &mut applicability).0;
span_lint_and_sugg(
cx,
DOUBLE_PARENS,
arg.span,
"unnecessary parentheses",
"remove them",
sugg.to_string(),
applicability,
);
}
let mut applicability = Applicability::MachineApplicable;
let sugg = snippet_with_context(cx.sess(), inner.span, arg.span.ctxt(), "_", &mut applicability).0;
span_lint_and_sugg(
cx,
DOUBLE_PARENS,
arg.span,
"unnecessary parentheses",
"remove them",
sugg.to_string(),
applicability,
);
},
_ => {},
}
Expand Down
6 changes: 2 additions & 4 deletions clippy_lints/src/infinite_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,8 @@ fn complete_infinite_iter(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
}
}
},
ExprKind::Binary(op, l, r) => {
if op.node.is_comparison() {
return is_infinite(cx, l).and(is_infinite(cx, r)).and(MaybeInfinite);
}
ExprKind::Binary(op, l, r) if op.node.is_comparison() => {
return is_infinite(cx, l).and(is_infinite(cx, r)).and(MaybeInfinite);
}, // TODO: ExprKind::Loop + Match
_ => (),
}
Expand Down
24 changes: 11 additions & 13 deletions clippy_lints/src/loops/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,17 @@ impl<'tcx> Visitor<'tcx> for IncrementVisitor<'_, 'tcx> {
}

match parent.kind {
ExprKind::AssignOp(op, lhs, rhs) => {
if lhs.hir_id == expr.hir_id {
*state = if op.node == AssignOpKind::AddAssign
&& is_integer_const(self.cx, rhs, 1)
&& *state == IncrementVisitorVarState::Initial
&& self.depth == 0
{
IncrementVisitorVarState::IncrOnce
} else {
// Assigned some other value or assigned multiple times
IncrementVisitorVarState::DontWarn
};
}
ExprKind::AssignOp(op, lhs, rhs) if lhs.hir_id == expr.hir_id => {
*state = if op.node == AssignOpKind::AddAssign
&& is_integer_const(self.cx, rhs, 1)
&& *state == IncrementVisitorVarState::Initial
&& self.depth == 0
{
IncrementVisitorVarState::IncrOnce
} else {
// Assigned some other value or assigned multiple times
IncrementVisitorVarState::DontWarn
};
},
ExprKind::Assign(lhs, _, _) if lhs.hir_id == expr.hir_id => {
*state = IncrementVisitorVarState::DontWarn;
Expand Down
39 changes: 14 additions & 25 deletions clippy_lints/src/manual_option_as_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,11 @@ impl LateLintPass<'_> for ManualOptionAsSlice {
return;
}
match expr.kind {
ExprKind::Match(scrutinee, [arm1, arm2], _) => {
ExprKind::Match(scrutinee, [arm1, arm2], _)
if is_none_pattern(cx, arm2.pat) && check_arms(cx, arm2, arm1)
|| is_none_pattern(cx, arm1.pat) && check_arms(cx, arm1, arm2)
{
check_as_ref(cx, scrutinee, span, self.msrv);
}
|| is_none_pattern(cx, arm1.pat) && check_arms(cx, arm1, arm2) =>
{
check_as_ref(cx, scrutinee, span, self.msrv);
},
ExprKind::If(cond, then, Some(other)) => {
if let ExprKind::Let(let_expr) = cond.kind
Expand All @@ -75,34 +74,24 @@ impl LateLintPass<'_> for ManualOptionAsSlice {
check_as_ref(cx, let_expr.init, span, self.msrv);
}
},
ExprKind::MethodCall(seg, callee, [], _) => {
if seg.ident.name == sym::unwrap_or_default {
check_map(cx, callee, span, self.msrv);
}
ExprKind::MethodCall(seg, callee, [], _) if seg.ident.name == sym::unwrap_or_default => {
check_map(cx, callee, span, self.msrv);
},
ExprKind::MethodCall(seg, callee, [or], _) => match seg.ident.name {
sym::unwrap_or => {
if is_empty_slice(cx, or) {
check_map(cx, callee, span, self.msrv);
}
sym::unwrap_or if is_empty_slice(cx, or) => {
check_map(cx, callee, span, self.msrv);
},
sym::unwrap_or_else => {
if returns_empty_slice(cx, or) {
check_map(cx, callee, span, self.msrv);
}
sym::unwrap_or_else if returns_empty_slice(cx, or) => {
check_map(cx, callee, span, self.msrv);
},
_ => {},
},
ExprKind::MethodCall(seg, callee, [or_else, map], _) => match seg.ident.name {
sym::map_or => {
if is_empty_slice(cx, or_else) && is_slice_from_ref(cx, map) {
check_as_ref(cx, callee, span, self.msrv);
}
sym::map_or if is_empty_slice(cx, or_else) && is_slice_from_ref(cx, map) => {
check_as_ref(cx, callee, span, self.msrv);
},
sym::map_or_else => {
if returns_empty_slice(cx, or_else) && is_slice_from_ref(cx, map) {
check_as_ref(cx, callee, span, self.msrv);
}
sym::map_or_else if returns_empty_slice(cx, or_else) && is_slice_from_ref(cx, map) => {
check_as_ref(cx, callee, span, self.msrv);
},
_ => {},
},
Expand Down
21 changes: 10 additions & 11 deletions clippy_lints/src/manual_strip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,13 @@ fn find_stripping<'tcx>(
&& self.cx.qpath_res(path, ex.hir_id) == self.target
{
match (self.strip_kind, start, end) {
(StripKind::Prefix, Some(start), None) => {
if eq_pattern_length(self.cx, self.pattern, start, self.ctxt) {
self.results.push(ex);
return;
}
(StripKind::Prefix, Some(start), None)
if eq_pattern_length(self.cx, self.pattern, start, self.ctxt) =>
{
self.results.push(ex);
return;
},
(StripKind::Suffix, None, Some(end)) => {
(StripKind::Suffix, None, Some(end))
if let ExprKind::Binary(
Spanned {
node: BinOpKind::Sub, ..
Expand All @@ -259,11 +259,10 @@ fn find_stripping<'tcx>(
&& let Some(left_arg) = len_arg(self.cx, left)
&& let ExprKind::Path(left_path) = &left_arg.kind
&& self.cx.qpath_res(left_path, left_arg.hir_id) == self.target
&& eq_pattern_length(self.cx, self.pattern, right, self.ctxt)
{
self.results.push(ex);
return;
}
&& eq_pattern_length(self.cx, self.pattern, right, self.ctxt) =>
{
self.results.push(ex);
return;
},
_ => {},
}
Expand Down
Loading