]> git.lizzy.rs Git - rust.git/commitdiff
Initial commit of unit expr
authorZaki Manian <zaki@manian.org>
Sat, 2 Sep 2017 18:25:33 +0000 (11:25 -0700)
committerZaki Manian <zaki@manian.org>
Sat, 2 Sep 2017 18:25:33 +0000 (11:25 -0700)
clippy_lints/src/is_unit_expr.rs [new file with mode: 0644]
clippy_lints/src/lib.rs

diff --git a/clippy_lints/src/is_unit_expr.rs b/clippy_lints/src/is_unit_expr.rs
new file mode 100644 (file)
index 0000000..4547630
--- /dev/null
@@ -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!();
+        }
+    }
+}
index 19b8816cd1e0cd6f3052f8e28d46dd5868a540bf..0324ff46b619209f245e5ee898e11abc2b484c20 100644 (file)
@@ -91,6 +91,7 @@ macro_rules! declare_restriction_lint {
 pub mod identity_op;
 pub mod if_let_redundant_pattern_matching;
 pub mod if_not_else;
+pub mod is_unit_expr;
 pub mod infinite_iter;
 pub mod items_after_statements;
 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 misc::Pass);
     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_late_lint_pass(box eta_reduction::EtaPass);
     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,
         partialeq_ne_impl::PARTIALEQ_NE_IMPL,
         precedence::PRECEDENCE,
+        is_unit_expr::UnitExpr,
         print::PRINT_WITH_NEWLINE,
         ptr::CMP_NULL,
         ptr::MUT_FROM_REF,