2015-12-05 20:21:34 -06:00
|
|
|
use rustc::lint::*;
|
2016-01-03 07:00:42 -06:00
|
|
|
use std::collections::HashMap;
|
2015-12-05 20:21:34 -06:00
|
|
|
use syntax::ast::*;
|
2016-01-03 07:00:42 -06:00
|
|
|
use syntax::codemap::Span;
|
|
|
|
use syntax::visit::FnKind;
|
2016-03-03 13:14:49 -06:00
|
|
|
use utils::{span_lint, span_help_and_lint, snippet, span_lint_and_then};
|
2016-02-05 17:41:54 -06:00
|
|
|
/// **What it does:** This lint checks for structure field patterns bound to wildcards.
|
2015-12-14 15:16:56 -06:00
|
|
|
///
|
|
|
|
/// **Why is this bad?** Using `..` instead is shorter and leaves the focus on the fields that are actually bound.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:** `let { a: _, b: ref b, c: _ } = ..`
|
2016-02-05 17:13:29 -06:00
|
|
|
declare_lint! {
|
|
|
|
pub UNNEEDED_FIELD_PATTERN, Warn,
|
|
|
|
"Struct fields are bound to a wildcard instead of using `..`"
|
|
|
|
}
|
2015-12-05 20:21:34 -06:00
|
|
|
|
2016-02-05 17:41:54 -06:00
|
|
|
/// **What it does:** This lint checks for function arguments having the similar names differing by an underscore
|
2016-01-03 07:00:42 -06:00
|
|
|
///
|
2016-01-03 09:55:09 -06:00
|
|
|
/// **Why is this bad?** It affects code readability
|
2016-01-03 07:00:42 -06:00
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:** `fn foo(a: i32, _a: i32) {}`
|
2016-02-05 17:13:29 -06:00
|
|
|
declare_lint! {
|
|
|
|
pub DUPLICATE_UNDERSCORE_ARGUMENT, Warn,
|
|
|
|
"Function arguments having names which only differ by an underscore"
|
|
|
|
}
|
2016-01-03 07:00:42 -06:00
|
|
|
|
2016-03-03 13:14:49 -06:00
|
|
|
/// **What it does:** This lint 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:** `(|| 42)()`
|
|
|
|
declare_lint! {
|
|
|
|
pub REDUNDANT_CLOSURE_CALL, Warn,
|
|
|
|
"Closures should not be called in the expression they are defined"
|
|
|
|
}
|
|
|
|
|
2015-12-05 20:21:34 -06:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct MiscEarly;
|
|
|
|
|
|
|
|
impl LintPass for MiscEarly {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
2016-03-03 13:14:49 -06:00
|
|
|
lint_array!(UNNEEDED_FIELD_PATTERN, DUPLICATE_UNDERSCORE_ARGUMENT, REDUNDANT_CLOSURE_CALL)
|
2015-12-05 20:21:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for MiscEarly {
|
|
|
|
fn check_pat(&mut self, cx: &EarlyContext, pat: &Pat) {
|
2016-02-17 06:38:44 -06:00
|
|
|
if let PatKind::Struct(ref npat, ref pfields, _) = pat.node {
|
2015-12-05 20:21:34 -06:00
|
|
|
let mut wilds = 0;
|
2016-02-20 14:03:45 -06:00
|
|
|
let type_name = npat.segments.last().expect("A path must have at least one segment").identifier.name;
|
2015-12-05 20:21:34 -06:00
|
|
|
|
|
|
|
for field in pfields {
|
2016-02-17 06:38:44 -06:00
|
|
|
if field.node.pat.node == PatKind::Wild {
|
2015-12-05 20:21:34 -06:00
|
|
|
wilds += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !pfields.is_empty() && wilds == pfields.len() {
|
2016-01-03 22:26:12 -06:00
|
|
|
span_help_and_lint(cx,
|
|
|
|
UNNEEDED_FIELD_PATTERN,
|
|
|
|
pat.span,
|
|
|
|
"All the struct fields are matched to a wildcard pattern, consider using `..`.",
|
|
|
|
&format!("Try with `{} {{ .. }}` instead", type_name));
|
2015-12-05 20:21:34 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if wilds > 0 {
|
2016-01-03 22:26:12 -06:00
|
|
|
let mut normal = vec![];
|
2016-01-01 22:52:13 -06:00
|
|
|
|
|
|
|
for field in pfields {
|
2016-02-17 06:38:44 -06:00
|
|
|
if field.node.pat.node != PatKind::Wild {
|
2016-01-01 22:52:13 -06:00
|
|
|
if let Ok(n) = cx.sess().codemap().span_to_snippet(field.span) {
|
|
|
|
normal.push(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-05 20:21:34 -06:00
|
|
|
for field in pfields {
|
2016-02-17 06:38:44 -06:00
|
|
|
if field.node.pat.node == PatKind::Wild {
|
2016-01-01 22:52:13 -06:00
|
|
|
wilds -= 1;
|
|
|
|
if wilds > 0 {
|
2016-01-03 22:26:12 -06:00
|
|
|
span_lint(cx,
|
|
|
|
UNNEEDED_FIELD_PATTERN,
|
|
|
|
field.span,
|
|
|
|
"You matched a field with a wildcard pattern. Consider using `..` instead");
|
2016-01-01 22:52:13 -06:00
|
|
|
} else {
|
2016-01-03 22:26:12 -06:00
|
|
|
span_help_and_lint(cx,
|
|
|
|
UNNEEDED_FIELD_PATTERN,
|
|
|
|
field.span,
|
|
|
|
"You matched a field with a wildcard pattern. Consider using `..` \
|
|
|
|
instead",
|
2016-01-01 22:52:13 -06:00
|
|
|
&format!("Try with `{} {{ {}, .. }}`",
|
|
|
|
type_name,
|
|
|
|
normal[..].join(", ")));
|
|
|
|
}
|
2015-12-05 20:21:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-03 07:00:42 -06:00
|
|
|
|
|
|
|
fn check_fn(&mut self, cx: &EarlyContext, _: FnKind, decl: &FnDecl, _: &Block, _: Span, _: NodeId) {
|
2016-01-03 22:26:12 -06:00
|
|
|
let mut registered_names: HashMap<String, Span> = HashMap::new();
|
2016-01-03 07:00:42 -06:00
|
|
|
|
|
|
|
for ref arg in &decl.inputs {
|
2016-02-17 06:38:44 -06:00
|
|
|
if let PatKind::Ident(_, sp_ident, None) = arg.pat.node {
|
2016-01-03 09:55:09 -06:00
|
|
|
let arg_name = sp_ident.node.to_string();
|
2016-01-03 07:00:42 -06:00
|
|
|
|
2016-02-14 21:40:43 -06:00
|
|
|
if arg_name.starts_with('_') {
|
2016-03-19 09:06:56 -05:00
|
|
|
if let Some(correspondence) = registered_names.get(&arg_name[1..]) {
|
2016-01-03 22:26:12 -06:00
|
|
|
span_lint(cx,
|
|
|
|
DUPLICATE_UNDERSCORE_ARGUMENT,
|
2016-03-19 09:06:56 -05:00
|
|
|
*correspondence,
|
2016-01-03 22:26:12 -06:00
|
|
|
&format!("`{}` already exists, having another argument having almost the same \
|
|
|
|
name makes code comprehension and documentation more difficult",
|
2016-03-03 13:14:49 -06:00
|
|
|
arg_name[1..].to_owned()));;
|
2016-01-03 09:55:09 -06:00
|
|
|
}
|
|
|
|
} else {
|
2016-02-02 15:35:01 -06:00
|
|
|
registered_names.insert(arg_name, arg.pat.span);
|
2016-01-03 07:00:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-03 13:14:49 -06:00
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
|
|
|
|
if let ExprKind::Call(ref paren, _) = expr.node {
|
|
|
|
if let ExprKind::Paren(ref closure) = paren.node {
|
|
|
|
if let ExprKind::Closure(_, ref decl, ref block) = closure.node {
|
|
|
|
span_lint_and_then(cx,
|
|
|
|
REDUNDANT_CLOSURE_CALL,
|
|
|
|
expr.span,
|
|
|
|
"Try not to call a closure in the expression where it is declared.",
|
|
|
|
|db| {
|
2016-03-06 07:10:04 -06:00
|
|
|
if decl.inputs.is_empty() {
|
2016-03-03 13:14:49 -06:00
|
|
|
let hint = format!("{}", snippet(cx, block.span, ".."));
|
|
|
|
db.span_suggestion(expr.span, "Try doing something like: ", hint);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_block(&mut self, cx: &EarlyContext, block: &Block) {
|
|
|
|
for w in block.stmts.windows(2) {
|
|
|
|
if_let_chain! {[
|
|
|
|
let StmtKind::Decl(ref first, _) = w[0].node,
|
|
|
|
let DeclKind::Local(ref local) = first.node,
|
|
|
|
let Option::Some(ref t) = local.init,
|
|
|
|
let ExprKind::Closure(_,_,_) = t.node,
|
|
|
|
let PatKind::Ident(_,sp_ident,_) = local.pat.node,
|
|
|
|
let StmtKind::Semi(ref second,_) = w[1].node,
|
|
|
|
let ExprKind::Assign(_,ref call) = second.node,
|
|
|
|
let ExprKind::Call(ref closure,_) = call.node,
|
|
|
|
let ExprKind::Path(_,ref path) = closure.node
|
|
|
|
], {
|
|
|
|
if sp_ident.node == (&path.segments[0]).identifier {
|
|
|
|
span_lint(cx, REDUNDANT_CLOSURE_CALL, second.span, "Closure called just once immediately after it was declared");
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
}
|
2015-12-05 20:21:34 -06:00
|
|
|
}
|