mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-24 13:43:17 +00:00
Move let_unit_value to its own module
This commit is contained in:
parent
37bffb7797
commit
d17f54538f
3 changed files with 47 additions and 34 deletions
|
@ -1085,7 +1085,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
|||
store.register_late_pass(|| box map_clone::MapClone);
|
||||
store.register_late_pass(|| box map_err_ignore::MapErrIgnore);
|
||||
store.register_late_pass(|| box shadow::Shadow);
|
||||
store.register_late_pass(|| box unit_types::LetUnitValue);
|
||||
store.register_late_pass(|| box unit_types::UnitTypes);
|
||||
store.register_late_pass(|| box unit_types::UnitCmp);
|
||||
store.register_late_pass(|| box loops::Loops);
|
||||
store.register_late_pass(|| box main_recursion::MainRecursion::default());
|
||||
|
|
40
clippy_lints/src/unit_types/let_unit_value.rs
Normal file
40
clippy_lints/src/unit_types/let_unit_value.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{Stmt, StmtKind};
|
||||
use rustc_lint::{LateContext, LintContext};
|
||||
use rustc_middle::lint::in_external_macro;
|
||||
|
||||
use crate::utils::diagnostics::span_lint_and_then;
|
||||
use crate::utils::higher;
|
||||
use crate::utils::source::snippet_with_macro_callsite;
|
||||
|
||||
use super::{utils, LET_UNIT_VALUE};
|
||||
|
||||
pub(super) fn check(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
|
||||
if let StmtKind::Local(ref local) = stmt.kind {
|
||||
if utils::is_unit(cx.typeck_results().pat_ty(&local.pat)) {
|
||||
if in_external_macro(cx.sess(), stmt.span) || local.pat.span.from_expansion() {
|
||||
return;
|
||||
}
|
||||
if higher::is_from_for_desugar(local) {
|
||||
return;
|
||||
}
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
LET_UNIT_VALUE,
|
||||
stmt.span,
|
||||
"this let-binding has unit value",
|
||||
|diag| {
|
||||
if let Some(expr) = &local.init {
|
||||
let snip = snippet_with_macro_callsite(cx, expr.span, "()");
|
||||
diag.span_suggestion(
|
||||
stmt.span,
|
||||
"omit the `let` binding",
|
||||
format!("{};", snip),
|
||||
Applicability::MachineApplicable, // snippet
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,18 +1,17 @@
|
|||
mod let_unit_value;
|
||||
mod utils;
|
||||
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, MatchSource, Node, Stmt, StmtKind};
|
||||
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
||||
use rustc_middle::lint::in_external_macro;
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
use rustc_span::hygiene::{ExpnKind, MacroKind};
|
||||
|
||||
use if_chain::if_chain;
|
||||
|
||||
use crate::utils::diagnostics::{span_lint, span_lint_and_then};
|
||||
use crate::utils::higher;
|
||||
use crate::utils::source::{indent_of, reindent_multiline, snippet_opt, snippet_with_macro_callsite};
|
||||
use crate::utils::source::{indent_of, reindent_multiline, snippet_opt};
|
||||
|
||||
use utils::{is_unit, is_unit_literal};
|
||||
|
||||
|
@ -35,37 +34,11 @@ declare_clippy_lint! {
|
|||
"creating a `let` binding to a value of unit type, which usually can't be used afterwards"
|
||||
}
|
||||
|
||||
declare_lint_pass!(LetUnitValue => [LET_UNIT_VALUE]);
|
||||
declare_lint_pass!(UnitTypes => [LET_UNIT_VALUE]);
|
||||
|
||||
impl<'tcx> LateLintPass<'tcx> for LetUnitValue {
|
||||
impl<'tcx> LateLintPass<'tcx> for UnitTypes {
|
||||
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
|
||||
if let StmtKind::Local(ref local) = stmt.kind {
|
||||
if is_unit(cx.typeck_results().pat_ty(&local.pat)) {
|
||||
if in_external_macro(cx.sess(), stmt.span) || local.pat.span.from_expansion() {
|
||||
return;
|
||||
}
|
||||
if higher::is_from_for_desugar(local) {
|
||||
return;
|
||||
}
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
LET_UNIT_VALUE,
|
||||
stmt.span,
|
||||
"this let-binding has unit value",
|
||||
|diag| {
|
||||
if let Some(expr) = &local.init {
|
||||
let snip = snippet_with_macro_callsite(cx, expr.span, "()");
|
||||
diag.span_suggestion(
|
||||
stmt.span,
|
||||
"omit the `let` binding",
|
||||
format!("{};", snip),
|
||||
Applicability::MachineApplicable, // snippet
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
let_unit_value::check(cx, stmt);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue