Merge branch 'pr-138'

Conflicts:
	src/lib.rs
This commit is contained in:
Manish Goregaokar 2015-08-12 21:22:01 +05:30
commit c58f803511
3 changed files with 49 additions and 2 deletions

View File

@ -58,6 +58,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
reg.register_lint_pass(box strings::StringAdd as LintPassObject);
reg.register_lint_pass(box returns::ReturnPass as LintPassObject);
reg.register_lint_pass(box methods::MethodsPass as LintPassObject);
reg.register_lint_pass(box types::LetPass as LintPassObject);
reg.register_lint_group("clippy", vec![types::BOX_VEC, types::LINKEDLIST,
misc::SINGLE_MATCH,
@ -84,5 +85,6 @@ pub fn plugin_registrar(reg: &mut Registry) {
methods::RESULT_UNWRAP_USED,
methods::STR_TO_STRING,
methods::STRING_TO_STRING,
types::LET_UNIT_VALUE,
]);
}

View File

@ -1,10 +1,11 @@
use syntax::ptr::P;
use syntax::ast;
use syntax::ast::*;
use rustc::middle::ty;
use rustc::lint::{Context, LintPass, LintArray, Lint, Level};
use syntax::codemap::Span;
use syntax::codemap::{ExpnInfo, Span};
use utils::{span_lint, span_help_and_lint};
use utils::{in_macro, snippet, span_lint, span_help_and_lint};
/// Handles all the linting of funky types
#[allow(missing_copy_implementations)]
@ -75,3 +76,34 @@ impl LintPass for TypePass {
}
}
}
#[allow(missing_copy_implementations)]
pub struct LetPass;
declare_lint!(pub LET_UNIT_VALUE, Warn,
"Warn on let-binding a value of unit type");
fn check_let_unit(cx: &Context, decl: &Decl, info: Option<&ExpnInfo>) {
if in_macro(cx, info) { return; }
if let DeclLocal(ref local) = decl.node {
let bindtype = &cx.tcx.pat_ty(&*local.pat).sty;
if *bindtype == ty::TyTuple(vec![]) {
span_lint(cx, LET_UNIT_VALUE, decl.span, &format!(
"this let-binding has unit value. Consider omitting `let {} =`.",
snippet(cx, local.pat.span, "..")));
}
}
}
impl LintPass for LetPass {
fn get_lints(&self) -> LintArray {
lint_array!(LET_UNIT_VALUE)
}
fn check_decl(&mut self, cx: &Context, decl: &Decl) {
cx.sess().codemap().with_expn_info(
decl.span.expn_id,
|info| check_let_unit(cx, decl, info));
}
}

13
tests/compile-fail/let_unit.rs Executable file
View File

@ -0,0 +1,13 @@
#![feature(plugin)]
#![plugin(clippy)]
#![deny(let_unit_value)]
fn main() {
let _x = println!("x"); //~ERROR this let-binding has unit value
let _y = 1; // this is fine
let _z = ((), 1); // this as well
if true {
let _a = (); //~ERROR
}
}