Merge pull request #25 from Twisol/lint-toplevel-ref-arg

Check for args like `fn foo(ref x: u8)`, where the `ref` is useless
This commit is contained in:
Manish Goregaokar 2014-12-25 05:42:37 +05:30
commit 6f9984981e
2 changed files with 28 additions and 3 deletions

View File

@ -1,8 +1,8 @@
#![feature(globs, phase, plugin_registrar)]
#![feature(globs, phase, plugin_registrar)]
#![allow(unused_imports)]
#[phase(plugin,link)]
#[phase(plugin, link)]
extern crate syntax;
#[phase(plugin, link)]
extern crate rustc;
@ -21,4 +21,5 @@ pub fn plugin_registrar(reg: &mut Registry) {
reg.register_lint_pass(box types::TypePass as LintPassObject);
reg.register_lint_pass(box misc::MiscPass as LintPassObject);
reg.register_lint_pass(box misc::StrToStringPass as LintPassObject);
reg.register_lint_pass(box misc::TopLevelRefPass as LintPassObject);
}

View File

@ -1,6 +1,7 @@
use syntax::ptr::P;
use syntax::ast;
use syntax::ast::*;
use syntax::visit::{FnKind};
use rustc::lint::{Context, LintPass, LintArray, Lint, Level};
use rustc::middle::ty::{mod, expr_ty, ty_str, ty_ptr, ty_rptr};
use syntax::codemap::Span;
@ -38,7 +39,7 @@ impl LintPass for MiscPass {
format!("Try if let {} = {} {{ ... }}",
map.span_to_snippet(arms[0].pats[0].span).unwrap_or("..".to_string()),
map.span_to_snippet(ex.span).unwrap_or("..".to_string())).as_slice()
);
);
}
}
}
@ -81,3 +82,26 @@ impl LintPass for StrToStringPass {
}
}
}
declare_lint!(CLIPPY_TOPLEVEL_REF_ARG, Warn, "Warn about pattern matches with top-level `ref` bindings");
pub struct TopLevelRefPass;
impl LintPass for TopLevelRefPass {
fn get_lints(&self) -> LintArray {
lint_array!(CLIPPY_TOPLEVEL_REF_ARG)
}
fn check_fn(&mut self, cx: &Context, _: FnKind, decl: &FnDecl, _: &Block, _: Span, _: NodeId) {
for ref arg in decl.inputs.iter() {
if let PatIdent(BindByRef(_), _, _) = arg.pat.node {
cx.span_lint(
CLIPPY_TOPLEVEL_REF_ARG,
arg.pat.span,
"`ref` directly on a function argument is ignored. Have you considered using a reference type instead?"
);
}
}
}
}