Move unit_cmp to its own module

This commit is contained in:
Yoshitomo Nakanishi 2021-03-12 12:38:31 +09:00
parent d17f54538f
commit 1bb221243b
3 changed files with 68 additions and 61 deletions

View file

@ -1086,7 +1086,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|| box map_err_ignore::MapErrIgnore);
store.register_late_pass(|| box shadow::Shadow);
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());
store.register_late_pass(|| box lifetimes::Lifetimes);

View file

@ -1,16 +1,16 @@
mod let_unit_value;
mod unit_cmp;
mod utils;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, MatchSource, Node, Stmt, StmtKind};
use rustc_hir::{Block, Expr, ExprKind, MatchSource, Node, Stmt, StmtKind};
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::diagnostics::span_lint_and_then;
use crate::utils::source::{indent_of, reindent_multiline, snippet_opt};
use utils::{is_unit, is_unit_literal};
@ -34,14 +34,6 @@ declare_clippy_lint! {
"creating a `let` binding to a value of unit type, which usually can't be used afterwards"
}
declare_lint_pass!(UnitTypes => [LET_UNIT_VALUE]);
impl<'tcx> LateLintPass<'tcx> for UnitTypes {
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
let_unit_value::check(cx, stmt);
}
}
declare_clippy_lint! {
/// **What it does:** Checks for comparisons to unit. This includes all binary
/// comparisons (like `==` and `<`) and asserts.
@ -89,56 +81,15 @@ declare_clippy_lint! {
"comparing unit values"
}
declare_lint_pass!(UnitCmp => [UNIT_CMP]);
declare_lint_pass!(UnitTypes => [LET_UNIT_VALUE, UNIT_CMP]);
impl<'tcx> LateLintPass<'tcx> for UnitCmp {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
if expr.span.from_expansion() {
if let Some(callee) = expr.span.source_callee() {
if let ExpnKind::Macro(MacroKind::Bang, symbol) = callee.kind {
if let ExprKind::Binary(ref cmp, ref left, _) = expr.kind {
let op = cmp.node;
if op.is_comparison() && is_unit(cx.typeck_results().expr_ty(left)) {
let result = match &*symbol.as_str() {
"assert_eq" | "debug_assert_eq" => "succeed",
"assert_ne" | "debug_assert_ne" => "fail",
_ => return,
};
span_lint(
cx,
UNIT_CMP,
expr.span,
&format!(
"`{}` of unit values detected. This will always {}",
symbol.as_str(),
result
),
);
}
}
}
}
return;
}
if let ExprKind::Binary(ref cmp, ref left, _) = expr.kind {
let op = cmp.node;
if op.is_comparison() && is_unit(cx.typeck_results().expr_ty(left)) {
let result = match op {
BinOpKind::Eq | BinOpKind::Le | BinOpKind::Ge => "true",
_ => "false",
};
span_lint(
cx,
UNIT_CMP,
expr.span,
&format!(
"{}-comparison of unit values detected. This will always be {}",
op.as_str(),
result
),
);
}
}
impl LateLintPass<'_> for UnitTypes {
fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) {
let_unit_value::check(cx, stmt);
}
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
unit_cmp::check(cx, expr);
}
}

View file

@ -0,0 +1,57 @@
use rustc_hir::{BinOpKind, Expr, ExprKind};
use rustc_lint::LateContext;
use rustc_span::hygiene::{ExpnKind, MacroKind};
use crate::utils::diagnostics::span_lint;
use super::{utils, UNIT_CMP};
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
if expr.span.from_expansion() {
if let Some(callee) = expr.span.source_callee() {
if let ExpnKind::Macro(MacroKind::Bang, symbol) = callee.kind {
if let ExprKind::Binary(ref cmp, ref left, _) = expr.kind {
let op = cmp.node;
if op.is_comparison() && utils::is_unit(cx.typeck_results().expr_ty(left)) {
let result = match &*symbol.as_str() {
"assert_eq" | "debug_assert_eq" => "succeed",
"assert_ne" | "debug_assert_ne" => "fail",
_ => return,
};
span_lint(
cx,
UNIT_CMP,
expr.span,
&format!(
"`{}` of unit values detected. This will always {}",
symbol.as_str(),
result
),
);
}
}
}
}
return;
}
if let ExprKind::Binary(ref cmp, ref left, _) = expr.kind {
let op = cmp.node;
if op.is_comparison() && utils::is_unit(cx.typeck_results().expr_ty(left)) {
let result = match op {
BinOpKind::Eq | BinOpKind::Le | BinOpKind::Ge => "true",
_ => "false",
};
span_lint(
cx,
UNIT_CMP,
expr.span,
&format!(
"{}-comparison of unit values detected. This will always be {}",
op.as_str(),
result
),
);
}
}
}