Skip to content

Commit f581aa7

Browse files
committed
Initial commit of unit expr
1 parent 3d62676 commit f581aa7

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

clippy_lints/src/is_unit_expr.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use rustc::lint::*;
2+
use syntax::ast::*;
3+
use syntax::codemap::Spanned;
4+
use utils::{span_lint_and_sugg, snippet};
5+
6+
7+
/// **What it does:** Checks for
8+
/// - () being assigned to a variable
9+
/// - () being passed to a function
10+
///
11+
/// **Why is this bad?** It is extremely unlikely that a user intended to assign '()' to valiable. Instead,
12+
/// Unit is what a block evaluates to when it returns nothing. This is typically caused by a trailing
13+
/// unintended semicolon.
14+
///
15+
/// **Known problems:** None.
16+
///
17+
/// **Example:**
18+
/// * `let x = {"foo" ;}` when the user almost certainly intended `let x ={"foo"}`
19+
20+
declare_lint! {
21+
pub UNIT_EXPR,
22+
Warn,
23+
"unintended assignment or use of a unit typed value"
24+
}
25+
26+
#[derive(Copy, Clone)]
27+
pub struct UnitExpr;
28+
29+
impl LintPass for UnitExpr {
30+
fn get_lints(&self) -> LintArray {
31+
lint_array!(UNIT_EXPR)
32+
}
33+
}
34+
35+
impl EarlyLintPass for UnitExpr {
36+
fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
37+
if let ExprKind::Assign(ref left, ref right) = expr.node {
38+
unimplemented!();
39+
}
40+
if let ExprKind::MethodCall(ref path, ref args) = expr.node {
41+
unimplemented!();
42+
}
43+
if let ExprKind::Call(ref path, ref args) = expr.node{
44+
unimplemented!();
45+
}
46+
}
47+
}

clippy_lints/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ pub mod functions;
9191
pub mod identity_op;
9292
pub mod if_let_redundant_pattern_matching;
9393
pub mod if_not_else;
94+
pub mod is_unit_expr;
9495
pub mod infinite_iter;
9596
pub mod items_after_statements;
9697
pub mod large_enum_variant;
@@ -233,6 +234,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
233234
reg.register_late_lint_pass(box approx_const::Pass);
234235
reg.register_late_lint_pass(box misc::Pass);
235236
reg.register_early_lint_pass(box precedence::Precedence);
237+
reg.register_early_lint_pass(box is_unit_expr::UnitExpr);
236238
reg.register_early_lint_pass(box needless_continue::NeedlessContinue);
237239
reg.register_late_lint_pass(box eta_reduction::EtaPass);
238240
reg.register_late_lint_pass(box identity_op::IdentityOp);
@@ -504,6 +506,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
504506
panic::PANIC_PARAMS,
505507
partialeq_ne_impl::PARTIALEQ_NE_IMPL,
506508
precedence::PRECEDENCE,
509+
is_unit_expr::UnitExpr,
507510
print::PRINT_WITH_NEWLINE,
508511
ptr::CMP_NULL,
509512
ptr::MUT_FROM_REF,

0 commit comments

Comments
 (0)