Skip to content

Commit d17f545

Browse files
committed
Move let_unit_value to its own module
1 parent 37bffb7 commit d17f545

File tree

3 files changed

+47
-34
lines changed

3 files changed

+47
-34
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10851085
store.register_late_pass(|| box map_clone::MapClone);
10861086
store.register_late_pass(|| box map_err_ignore::MapErrIgnore);
10871087
store.register_late_pass(|| box shadow::Shadow);
1088-
store.register_late_pass(|| box unit_types::LetUnitValue);
1088+
store.register_late_pass(|| box unit_types::UnitTypes);
10891089
store.register_late_pass(|| box unit_types::UnitCmp);
10901090
store.register_late_pass(|| box loops::Loops);
10911091
store.register_late_pass(|| box main_recursion::MainRecursion::default());
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use rustc_errors::Applicability;
2+
use rustc_hir::{Stmt, StmtKind};
3+
use rustc_lint::{LateContext, LintContext};
4+
use rustc_middle::lint::in_external_macro;
5+
6+
use crate::utils::diagnostics::span_lint_and_then;
7+
use crate::utils::higher;
8+
use crate::utils::source::snippet_with_macro_callsite;
9+
10+
use super::{utils, LET_UNIT_VALUE};
11+
12+
pub(super) fn check(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
13+
if let StmtKind::Local(ref local) = stmt.kind {
14+
if utils::is_unit(cx.typeck_results().pat_ty(&local.pat)) {
15+
if in_external_macro(cx.sess(), stmt.span) || local.pat.span.from_expansion() {
16+
return;
17+
}
18+
if higher::is_from_for_desugar(local) {
19+
return;
20+
}
21+
span_lint_and_then(
22+
cx,
23+
LET_UNIT_VALUE,
24+
stmt.span,
25+
"this let-binding has unit value",
26+
|diag| {
27+
if let Some(expr) = &local.init {
28+
let snip = snippet_with_macro_callsite(cx, expr.span, "()");
29+
diag.span_suggestion(
30+
stmt.span,
31+
"omit the `let` binding",
32+
format!("{};", snip),
33+
Applicability::MachineApplicable, // snippet
34+
);
35+
}
36+
},
37+
);
38+
}
39+
}
40+
}

clippy_lints/src/unit_types/mod.rs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1+
mod let_unit_value;
12
mod utils;
23

34
use rustc_errors::Applicability;
45
use rustc_hir as hir;
56
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, MatchSource, Node, Stmt, StmtKind};
6-
use rustc_lint::{LateContext, LateLintPass, LintContext};
7-
use rustc_middle::lint::in_external_macro;
7+
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
99
use rustc_span::hygiene::{ExpnKind, MacroKind};
1010

1111
use if_chain::if_chain;
1212

1313
use crate::utils::diagnostics::{span_lint, span_lint_and_then};
14-
use crate::utils::higher;
15-
use crate::utils::source::{indent_of, reindent_multiline, snippet_opt, snippet_with_macro_callsite};
14+
use crate::utils::source::{indent_of, reindent_multiline, snippet_opt};
1615

1716
use utils::{is_unit, is_unit_literal};
1817

@@ -35,37 +34,11 @@ declare_clippy_lint! {
3534
"creating a `let` binding to a value of unit type, which usually can't be used afterwards"
3635
}
3736

38-
declare_lint_pass!(LetUnitValue => [LET_UNIT_VALUE]);
37+
declare_lint_pass!(UnitTypes => [LET_UNIT_VALUE]);
3938

40-
impl<'tcx> LateLintPass<'tcx> for LetUnitValue {
39+
impl<'tcx> LateLintPass<'tcx> for UnitTypes {
4140
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
42-
if let StmtKind::Local(ref local) = stmt.kind {
43-
if is_unit(cx.typeck_results().pat_ty(&local.pat)) {
44-
if in_external_macro(cx.sess(), stmt.span) || local.pat.span.from_expansion() {
45-
return;
46-
}
47-
if higher::is_from_for_desugar(local) {
48-
return;
49-
}
50-
span_lint_and_then(
51-
cx,
52-
LET_UNIT_VALUE,
53-
stmt.span,
54-
"this let-binding has unit value",
55-
|diag| {
56-
if let Some(expr) = &local.init {
57-
let snip = snippet_with_macro_callsite(cx, expr.span, "()");
58-
diag.span_suggestion(
59-
stmt.span,
60-
"omit the `let` binding",
61-
format!("{};", snip),
62-
Applicability::MachineApplicable, // snippet
63-
);
64-
}
65-
},
66-
);
67-
}
68-
}
41+
let_unit_value::check(cx, stmt);
6942
}
7043
}
7144

0 commit comments

Comments
 (0)