-use crate::utils::{
- constants, snippet_opt, snippet_with_applicability, span_lint, span_lint_and_help, span_lint_and_sugg,
- span_lint_and_then,
-};
-use if_chain::if_chain;
+use crate::utils::{constants, snippet_opt, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
use rustc_ast::ast::{
- BindingMode, Block, Expr, ExprKind, GenericParamKind, Generics, Lit, LitFloatType, LitIntType, LitKind, Mutability,
- NodeId, Pat, PatKind, StmtKind, UnOp,
+ BindingMode, Expr, ExprKind, GenericParamKind, Generics, Lit, LitFloatType, LitIntType, LitKind, Mutability,
+ NodeId, Pat, PatKind, UnOp,
};
-use rustc_ast::visit::{walk_expr, FnKind, Visitor};
+use rustc_ast::visit::FnKind;
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
"function arguments having names which only differ by an underscore"
}
-declare_clippy_lint! {
- /// **What it does:** Detects closures called in the same expression where they
- /// are defined.
- ///
- /// **Why is this bad?** It is unnecessarily adding to the expression's
- /// complexity.
- ///
- /// **Known problems:** None.
- ///
- /// **Example:**
- /// ```rust,ignore
- /// // Bad
- /// let a = (|| 42)()
- ///
- /// // Good
- /// let a = 42
- /// ```
- pub REDUNDANT_CLOSURE_CALL,
- complexity,
- "throwaway closures called in the expression they are defined"
-}
-
declare_clippy_lint! {
/// **What it does:** Detects expressions of the form `--x`.
///
declare_lint_pass!(MiscEarlyLints => [
UNNEEDED_FIELD_PATTERN,
DUPLICATE_UNDERSCORE_ARGUMENT,
- REDUNDANT_CLOSURE_CALL,
DOUBLE_NEG,
MIXED_CASE_HEX_LITERALS,
UNSEPARATED_LITERAL_SUFFIX,
UNNEEDED_WILDCARD_PATTERN,
]);
-// Used to find `return` statements or equivalents e.g., `?`
-struct ReturnVisitor {
- found_return: bool,
-}
-
-impl ReturnVisitor {
- #[must_use]
- fn new() -> Self {
- Self { found_return: false }
- }
-}
-
-impl<'ast> Visitor<'ast> for ReturnVisitor {
- fn visit_expr(&mut self, ex: &'ast Expr) {
- if let ExprKind::Ret(_) = ex.kind {
- self.found_return = true;
- } else if let ExprKind::Try(_) = ex.kind {
- self.found_return = true;
- }
-
- walk_expr(self, ex)
- }
-}
-
impl EarlyLintPass for MiscEarlyLints {
fn check_generics(&mut self, cx: &EarlyContext<'_>, gen: &Generics) {
for param in &gen.params {
cx,
BUILTIN_TYPE_SHADOW,
param.ident.span,
- &format!("This generic shadows the built-in type `{}`", name),
+ &format!("this generic shadows the built-in type `{}`", name),
);
}
}
cx,
UNNEEDED_FIELD_PATTERN,
pat.span,
- "All the struct fields are matched to a wildcard pattern, consider using `..`.",
+ "all the struct fields are matched to a wildcard pattern, consider using `..`",
None,
- &format!("Try with `{} {{ .. }}` instead", type_name),
+ &format!("try with `{} {{ .. }}` instead", type_name),
);
return;
}
cx,
UNNEEDED_FIELD_PATTERN,
field.span,
- "You matched a field with a wildcard pattern. Consider using `..` instead",
+ "you matched a field with a wildcard pattern, consider using `..` instead",
);
} else {
let mut normal = vec![];
cx,
UNNEEDED_FIELD_PATTERN,
field.span,
- "You matched a field with a wildcard pattern. Consider using `..` \
+ "you matched a field with a wildcard pattern, consider using `..` \
instead",
None,
- &format!("Try with `{} {{ {}, .. }}`", type_name, normal[..].join(", ")),
+ &format!("try with `{} {{ {}, .. }}`", type_name, normal[..].join(", ")),
);
}
}
return;
}
match expr.kind {
- ExprKind::Call(ref paren, _) => {
- if let ExprKind::Paren(ref closure) = paren.kind {
- if let ExprKind::Closure(_, _, _, ref decl, ref block, _) = closure.kind {
- let mut visitor = ReturnVisitor::new();
- visitor.visit_expr(block);
- if !visitor.found_return {
- span_lint_and_then(
- cx,
- REDUNDANT_CLOSURE_CALL,
- expr.span,
- "Try not to call a closure in the expression where it is declared.",
- |diag| {
- if decl.inputs.is_empty() {
- let mut app = Applicability::MachineApplicable;
- let hint =
- snippet_with_applicability(cx, block.span, "..", &mut app).into_owned();
- diag.span_suggestion(expr.span, "Try doing something like: ", hint, app);
- }
- },
- );
- }
- }
- }
- },
ExprKind::Unary(UnOp::Neg, ref inner) => {
if let ExprKind::Unary(UnOp::Neg, _) = inner.kind {
span_lint(
_ => (),
}
}
-
- fn check_block(&mut self, cx: &EarlyContext<'_>, block: &Block) {
- for w in block.stmts.windows(2) {
- if_chain! {
- if let StmtKind::Local(ref local) = w[0].kind;
- if let Option::Some(ref t) = local.init;
- if let ExprKind::Closure(..) = t.kind;
- if let PatKind::Ident(_, ident, _) = local.pat.kind;
- if let StmtKind::Semi(ref second) = w[1].kind;
- if let ExprKind::Assign(_, ref call, _) = second.kind;
- if let ExprKind::Call(ref closure, _) = call.kind;
- if let ExprKind::Path(_, ref path) = closure.kind;
- then {
- if ident == path.segments[0].ident {
- span_lint(
- cx,
- REDUNDANT_CLOSURE_CALL,
- second.span,
- "Closure called just once immediately after it was declared",
- );
- }
- }
- }
- }
- }
}
impl MiscEarlyLints {
);
}
- #[allow(clippy::trivially_copy_pass_by_ref)]
- fn is_wild<P: std::ops::Deref<Target = Pat>>(pat: &&P) -> bool {
- if let PatKind::Wild = pat.kind {
- true
- } else {
- false
- }
- }
-
if let Some(rest_index) = patterns.iter().position(|pat| pat.is_rest()) {
if let Some((left_index, left_pat)) = patterns[..rest_index]
.iter()
.rev()
- .take_while(is_wild)
+ .take_while(|pat| matches!(pat.kind, PatKind::Wild))
.enumerate()
.last()
{
span_lint(cx, left_pat.span.until(patterns[rest_index].span), left_index == 0);
}
- if let Some((right_index, right_pat)) =
- patterns[rest_index + 1..].iter().take_while(is_wild).enumerate().last()
+ if let Some((right_index, right_pat)) = patterns[rest_index + 1..]
+ .iter()
+ .take_while(|pat| matches!(pat.kind, PatKind::Wild))
+ .enumerate()
+ .last()
{
span_lint(
cx,