Initial commit of unit expr
This commit is contained in:
47
clippy_lints/src/is_unit_expr.rs
Normal file
47
clippy_lints/src/is_unit_expr.rs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
use rustc::lint::*;
|
||||||
|
use syntax::ast::*;
|
||||||
|
use syntax::codemap::Spanned;
|
||||||
|
use utils::{span_lint_and_sugg, snippet};
|
||||||
|
|
||||||
|
|
||||||
|
/// **What it does:** Checks for
|
||||||
|
/// - () being assigned to a variable
|
||||||
|
/// - () being passed to a function
|
||||||
|
///
|
||||||
|
/// **Why is this bad?** It is extremely unlikely that a user intended to assign '()' to valiable. Instead,
|
||||||
|
/// Unit is what a block evaluates to when it returns nothing. This is typically caused by a trailing
|
||||||
|
/// unintended semicolon.
|
||||||
|
///
|
||||||
|
/// **Known problems:** None.
|
||||||
|
///
|
||||||
|
/// **Example:**
|
||||||
|
/// * `let x = {"foo" ;}` when the user almost certainly intended `let x ={"foo"}`
|
||||||
|
|
||||||
|
declare_lint! {
|
||||||
|
pub UNIT_EXPR,
|
||||||
|
Warn,
|
||||||
|
"unintended assignment or use of a unit typed value"
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
pub struct UnitExpr;
|
||||||
|
|
||||||
|
impl LintPass for UnitExpr {
|
||||||
|
fn get_lints(&self) -> LintArray {
|
||||||
|
lint_array!(UNIT_EXPR)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EarlyLintPass for UnitExpr {
|
||||||
|
fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
|
||||||
|
if let ExprKind::Assign(ref left, ref right) = expr.node {
|
||||||
|
unimplemented!();
|
||||||
|
}
|
||||||
|
if let ExprKind::MethodCall(ref path, ref args) = expr.node {
|
||||||
|
unimplemented!();
|
||||||
|
}
|
||||||
|
if let ExprKind::Call(ref path, ref args) = expr.node{
|
||||||
|
unimplemented!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -91,6 +91,7 @@ pub mod functions;
|
|||||||
pub mod identity_op;
|
pub mod identity_op;
|
||||||
pub mod if_let_redundant_pattern_matching;
|
pub mod if_let_redundant_pattern_matching;
|
||||||
pub mod if_not_else;
|
pub mod if_not_else;
|
||||||
|
pub mod is_unit_expr;
|
||||||
pub mod infinite_iter;
|
pub mod infinite_iter;
|
||||||
pub mod items_after_statements;
|
pub mod items_after_statements;
|
||||||
pub mod large_enum_variant;
|
pub mod large_enum_variant;
|
||||||
@@ -233,6 +234,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
|
|||||||
reg.register_late_lint_pass(box approx_const::Pass);
|
reg.register_late_lint_pass(box approx_const::Pass);
|
||||||
reg.register_late_lint_pass(box misc::Pass);
|
reg.register_late_lint_pass(box misc::Pass);
|
||||||
reg.register_early_lint_pass(box precedence::Precedence);
|
reg.register_early_lint_pass(box precedence::Precedence);
|
||||||
|
reg.register_early_lint_pass(box is_unit_expr::UnitExpr);
|
||||||
reg.register_early_lint_pass(box needless_continue::NeedlessContinue);
|
reg.register_early_lint_pass(box needless_continue::NeedlessContinue);
|
||||||
reg.register_late_lint_pass(box eta_reduction::EtaPass);
|
reg.register_late_lint_pass(box eta_reduction::EtaPass);
|
||||||
reg.register_late_lint_pass(box identity_op::IdentityOp);
|
reg.register_late_lint_pass(box identity_op::IdentityOp);
|
||||||
@@ -504,6 +506,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
|
|||||||
panic::PANIC_PARAMS,
|
panic::PANIC_PARAMS,
|
||||||
partialeq_ne_impl::PARTIALEQ_NE_IMPL,
|
partialeq_ne_impl::PARTIALEQ_NE_IMPL,
|
||||||
precedence::PRECEDENCE,
|
precedence::PRECEDENCE,
|
||||||
|
is_unit_expr::UnitExpr,
|
||||||
print::PRINT_WITH_NEWLINE,
|
print::PRINT_WITH_NEWLINE,
|
||||||
ptr::CMP_NULL,
|
ptr::CMP_NULL,
|
||||||
ptr::MUT_FROM_REF,
|
ptr::MUT_FROM_REF,
|
||||||
|
|||||||
Reference in New Issue
Block a user