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::print::pprust;
|
|
|
|
use syntax::visit::FnKind;
|
2015-12-05 20:21:34 -06:00
|
|
|
|
2016-01-01 22:52:13 -06:00
|
|
|
use utils::{span_lint, span_help_and_lint};
|
2015-12-05 20:21:34 -06:00
|
|
|
|
2015-12-14 15:16:56 -06:00
|
|
|
/// **What it does:** This lint `Warn`s on struct field patterns bound to wildcards.
|
|
|
|
///
|
|
|
|
/// **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: _ } = ..`
|
2015-12-07 06:23:52 -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-01-03 07:00:42 -06:00
|
|
|
/// **What it does:** This lint `Warn`s on function arguments having the same name except one starts with '_'
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** It makes source code documentation more difficult
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:** `fn foo(a: i32, _a: i32) {}`
|
|
|
|
declare_lint!(pub DUPLICATE_UNDERSCORE_ARGUMENT, Warn,
|
|
|
|
"Function arguments having names which only differ by an underscore");
|
|
|
|
|
2015-12-05 20:21:34 -06:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct MiscEarly;
|
|
|
|
|
|
|
|
impl LintPass for MiscEarly {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
2016-01-03 07:00:42 -06:00
|
|
|
lint_array!(UNNEEDED_FIELD_PATTERN, DUPLICATE_UNDERSCORE_ARGUMENT)
|
2015-12-05 20:21:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for MiscEarly {
|
|
|
|
fn check_pat(&mut self, cx: &EarlyContext, pat: &Pat) {
|
2016-01-01 22:52:13 -06:00
|
|
|
if let PatStruct(ref npat, ref pfields, _) = pat.node {
|
2015-12-05 20:21:34 -06:00
|
|
|
let mut wilds = 0;
|
2016-01-01 22:52:13 -06:00
|
|
|
let type_name = match npat.segments.last() {
|
|
|
|
Some(elem) => format!("{}", elem.identifier.name),
|
|
|
|
None => String::new(),
|
|
|
|
};
|
2015-12-05 20:21:34 -06:00
|
|
|
|
|
|
|
for field in pfields {
|
|
|
|
if field.node.pat.node == PatWild {
|
|
|
|
wilds += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !pfields.is_empty() && wilds == pfields.len() {
|
2016-01-01 22:52:13 -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-01 22:52:13 -06:00
|
|
|
let mut normal = vec!();
|
|
|
|
|
|
|
|
for field in pfields {
|
|
|
|
if field.node.pat.node != PatWild {
|
|
|
|
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 {
|
|
|
|
if field.node.pat.node == PatWild {
|
2016-01-01 22:52:13 -06:00
|
|
|
wilds -= 1;
|
|
|
|
if wilds > 0 {
|
|
|
|
span_lint(cx, UNNEEDED_FIELD_PATTERN, field.span,
|
|
|
|
"You matched a field with a wildcard pattern. \
|
|
|
|
Consider using `..` instead");
|
|
|
|
} else {
|
|
|
|
span_help_and_lint(cx, UNNEEDED_FIELD_PATTERN, field.span,
|
|
|
|
"You matched a field with a wildcard pattern. \
|
|
|
|
Consider using `..` instead",
|
|
|
|
&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) {
|
|
|
|
let mut registered_names : HashMap<String, Span> = HashMap::new();
|
|
|
|
|
|
|
|
for ref arg in &decl.inputs {
|
|
|
|
let arg_name = pprust::pat_to_string(&arg.pat);
|
|
|
|
|
|
|
|
if arg_name.starts_with("_") {
|
|
|
|
if let Some(correspondance) = registered_names.get(&arg_name[1..].to_owned()) {
|
|
|
|
span_lint(cx, DUPLICATE_UNDERSCORE_ARGUMENT, *correspondance,
|
|
|
|
&format!("`{}` already exists, having another argument having almost \
|
|
|
|
the same name makes code comprehension and documentation \
|
|
|
|
more difficult", arg_name[1..].to_owned()));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
registered_names.insert(arg_name.to_owned(), arg.pat.span.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-05 20:21:34 -06:00
|
|
|
}
|