Skip to content

Commit

Permalink
Fix internal lint checking match_type uses
Browse files Browse the repository at this point in the history
* Check for `is_item` instead
* Check consts and statics from external crates
* Check for lang items
* Check for inherent functions which have the same name as a field
  • Loading branch information
Jarcho committed Sep 8, 2021
1 parent dd8070d commit 3fc960d
Show file tree
Hide file tree
Showing 24 changed files with 406 additions and 239 deletions.
5 changes: 3 additions & 2 deletions clippy_lints/src/drop_forget_ref.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use clippy_utils::diagnostics::span_lint_and_note;
use clippy_utils::is_any_item;
use clippy_utils::ty::is_copy;
use clippy_utils::{is_any_item, paths};
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::sym;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -116,7 +117,7 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
if !(arg_ty.is_ref() || is_copy(cx, arg_ty)) {
return;
}
let (lint, msg) = match is_any_item(cx, path, &[paths::DROP.as_slice(), paths::MEM_FORGET.as_slice()]) {
let (lint, msg) = match is_any_item(cx, path, &[sym::mem_drop, sym::mem_forget]) {
Some(0) if arg_ty.is_ref() => (DROP_REF, DROP_REF_SUMMARY.to_string()),
Some(0) => (DROP_COPY, DROP_COPY_SUMMARY.to_string()),
Some(1) if arg_ty.is_ref() => (FORGET_REF, FORGET_REF_SUMMARY.to_string()),
Expand Down
9 changes: 4 additions & 5 deletions clippy_lints/src/duration_subsec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Spanned;
use rustc_span::{source_map::Spanned, sym};

use clippy_utils::consts::{constant, Constant};
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::paths;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -44,8 +43,8 @@ impl<'tcx> LateLintPass<'tcx> for DurationSubsec {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if_chain! {
if let ExprKind::Binary(Spanned { node: BinOpKind::Div, .. }, left, right) = expr.kind;
if let ExprKind::MethodCall(method_path, _ , args, _) = left.kind;
if is_item(cx, cx.typeck_results().expr_ty(&args[0]).peel_refs(), &paths::DURATION);
if let ExprKind::MethodCall(method_path, _ , [self_arg], _) = left.kind;
if is_item(cx, cx.typeck_results().expr_ty(self_arg).peel_refs(), sym::Duration);
if let Some((Constant::Int(divisor), _)) = constant(cx, cx.typeck_results(), right);
then {
let suggested_fn = match (method_path.ident.as_str().as_ref(), divisor) {
Expand All @@ -62,7 +61,7 @@ impl<'tcx> LateLintPass<'tcx> for DurationSubsec {
"try",
format!(
"{}.{}()",
snippet_with_applicability(cx, args[0].span, "_", &mut applicability),
snippet_with_applicability(cx, self_arg.span, "_", &mut applicability),
suggested_fn
),
applicability,
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/infinite_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ fn is_infinite(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
},
ExprKind::Block(block, _) => block.expr.as_ref().map_or(Finite, |e| is_infinite(cx, e)),
ExprKind::Box(e) | ExprKind::AddrOf(BorrowKind::Ref, _, e) => is_infinite(cx, e),
ExprKind::Call(path, _) => is_item(cx, path, &paths::ITER_REPEAT).into(),
ExprKind::Call(path, _) => is_item(cx, path, sym::iter_repeat).into(),
ExprKind::Struct(..) => higher::Range::hir(expr).map_or(false, |r| r.end.is_none()).into(),
_ => Finite,
}
Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,9 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
#[cfg(feature = "internal-lints")]
utils::internal_lints::INVALID_PATHS,
#[cfg(feature = "internal-lints")]
utils::internal_lints::LINT_WITHOUT_LINT_PASS,
utils::internal_lints::IS_ITEM_DEF_PATH_ON_DIAGNOSTIC_OR_LANG_ITEM,
#[cfg(feature = "internal-lints")]
utils::internal_lints::MATCH_TYPE_ON_DIAGNOSTIC_ITEM,
utils::internal_lints::LINT_WITHOUT_LINT_PASS,
#[cfg(feature = "internal-lints")]
utils::internal_lints::OUTER_EXPN_EXPN_DATA,
#[cfg(feature = "internal-lints")]
Expand Down Expand Up @@ -1171,8 +1171,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(utils::internal_lints::IF_CHAIN_STYLE),
LintId::of(utils::internal_lints::INTERNING_DEFINED_SYMBOL),
LintId::of(utils::internal_lints::INVALID_PATHS),
LintId::of(utils::internal_lints::IS_ITEM_DEF_PATH_ON_DIAGNOSTIC_OR_LANG_ITEM),
LintId::of(utils::internal_lints::LINT_WITHOUT_LINT_PASS),
LintId::of(utils::internal_lints::MATCH_TYPE_ON_DIAGNOSTIC_ITEM),
LintId::of(utils::internal_lints::OUTER_EXPN_EXPN_DATA),
LintId::of(utils::internal_lints::PRODUCE_ICE),
LintId::of(utils::internal_lints::UNNECESSARY_SYMBOL_STR),
Expand Down Expand Up @@ -1847,7 +1847,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|| Box::new(utils::internal_lints::InvalidPaths));
store.register_late_pass(|| Box::new(utils::internal_lints::InterningDefinedSymbol::default()));
store.register_late_pass(|| Box::new(utils::internal_lints::LintWithoutLintPass::default()));
store.register_late_pass(|| Box::new(utils::internal_lints::MatchTypeOnDiagItem));
store.register_late_pass(|| Box::new(utils::internal_lints::IsItemDefPath));
store.register_late_pass(|| Box::new(utils::internal_lints::OuterExpnDataPass));
}

Expand Down
5 changes: 3 additions & 2 deletions clippy_lints/src/mem_discriminant.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::is_item;
use clippy_utils::source::snippet;
use clippy_utils::ty::walk_ptrs_ty_depth;
use clippy_utils::{is_item, paths};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{BorrowKind, Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::sym;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -35,7 +36,7 @@ impl<'tcx> LateLintPass<'tcx> for MemDiscriminant {
if_chain! {
if let ExprKind::Call(func, func_args) = expr.kind;
// is `mem::discriminant`
if is_item(cx, func, &paths::MEM_DISCRIMINANT);
if is_item(cx, func, sym::mem_discriminant);
// type is non-enum
let ty_param = cx.typeck_results().node_substs(func.hir_id).type_at(0);
if !ty_param.is_enum();
Expand Down
5 changes: 3 additions & 2 deletions clippy_lints/src/mem_forget.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use clippy_utils::diagnostics::span_lint;
use clippy_utils::{is_item, paths};
use clippy_utils::is_item;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::sym;

declare_clippy_lint! {
/// ### What it does
Expand All @@ -29,7 +30,7 @@ declare_lint_pass!(MemForget => [MEM_FORGET]);
impl<'tcx> LateLintPass<'tcx> for MemForget {
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
if let ExprKind::Call(path_expr, [ref first_arg, ..]) = e.kind {
if is_item(cx, path_expr, &paths::MEM_FORGET) {
if is_item(cx, path_expr, sym::mem_forget) {
let forgot_ty = cx.typeck_results().expr_ty(first_arg);
if forgot_ty.ty_adt_def().map_or(false, |def| def.has_dtor(cx.tcx)) {
span_lint(cx, MEM_FORGET, e.span, "usage of `mem::forget` on `Drop` type");
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/mem_replace.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
use clippy_utils::source::{snippet, snippet_with_applicability};
use clippy_utils::ty::is_non_aggregate_primitive_type;
use clippy_utils::{in_macro, is_default_equivalent, is_item, is_lang_ctor, meets_msrv, msrvs, paths};
use clippy_utils::{in_macro, is_default_equivalent, is_item, is_lang_ctor, meets_msrv, msrvs};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::LangItem::OptionNone;
Expand Down Expand Up @@ -244,7 +244,7 @@ impl<'tcx> LateLintPass<'tcx> for MemReplace {
if_chain! {
// Check that `expr` is a call to `mem::replace()`
if let ExprKind::Call(func, [dest, src]) = expr.kind;
if is_item(cx, func, &paths::MEM_REPLACE);
if is_item(cx, func, sym::mem_replace);
then {
check_replace_option_with_none(cx, src, dest, expr.span);
check_replace_with_uninit(cx, src, dest, expr.span);
Expand Down
5 changes: 3 additions & 2 deletions clippy_lints/src/methods/filetype_is_file.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::{get_parent_expr, is_item, paths};
use clippy_utils::{get_parent_expr, is_item};
use if_chain::if_chain;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_span::source_map::Span;
use rustc_span::sym;

use super::FILETYPE_IS_FILE;

pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) {
let ty = cx.typeck_results().expr_ty(recv);

if !is_item(cx, ty, &paths::FILE_TYPE) {
if !is_item(cx, ty, sym::FileType) {
return;
}

Expand Down
16 changes: 9 additions & 7 deletions clippy_lints/src/methods/inefficient_to_string.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::walk_ptrs_ty_depth;
use clippy_utils::{is_item, paths};
use clippy_utils::{is_any_item, is_item, paths};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir as hir;
Expand Down Expand Up @@ -50,11 +50,13 @@ pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, method_name: Sy
/// Returns whether `ty` specializes `ToString`.
/// Currently, these are `str`, `String`, and `Cow<'_, str>`.
fn specializes_tostring(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
if ty.is_str() || is_item(cx, ty, sym::string_type) {
true
} else if let ty::Adt(adt, substs) = ty.kind() {
is_item(cx, adt.did, &paths::COW) && substs.type_at(1).is_str()
} else {
false
match *ty.kind() {
ty::Str => true,
ty::Adt(adt, subs) => match is_any_item(cx, adt.did, &[sym::string_type, sym::Cow]) {
Some(0) => true,
Some(1) => subs.type_at(1).is_str(),
_ => false,
},
_ => false,
}
}
29 changes: 13 additions & 16 deletions clippy_lints/src/methods/manual_str_repeat.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
use clippy_utils::sugg::Sugg;
use clippy_utils::{is_item, paths};
use clippy_utils::{is_any_item, is_item};
use if_chain::if_chain;
use rustc_ast::LitKind;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, LangItem};
use rustc_lint::LateContext;
use rustc_middle::ty::{self, Ty, TyS};
use rustc_middle::ty;
use rustc_span::symbol::sym;
use std::borrow::Cow;

Expand All @@ -18,14 +18,6 @@ enum RepeatKind {
Char(char),
}

fn get_ty_param(ty: Ty<'_>) -> Option<Ty<'_>> {
if let ty::Adt(_, subs) = ty.kind() {
subs.types().next()
} else {
None
}
}

fn parse_repeat_arg(cx: &LateContext<'_>, e: &Expr<'_>) -> Option<RepeatKind> {
if let ExprKind::Lit(lit) = &e.kind {
match lit.node {
Expand All @@ -34,11 +26,16 @@ fn parse_repeat_arg(cx: &LateContext<'_>, e: &Expr<'_>) -> Option<RepeatKind> {
_ => None,
}
} else {
let ty = cx.typeck_results().expr_ty(e).peel_refs();
(ty.is_str()
|| is_item(cx, ty, sym::string_type)
|| (is_item(cx, ty, LangItem::OwnedBox) && get_ty_param(ty).map_or(false, TyS::is_str))
|| (is_item(cx, ty, &paths::COW) && get_ty_param(ty).map_or(false, TyS::is_str)))
match cx.typeck_results().expr_ty(e).peel_refs().kind() {
ty::Str => true,
ty::Adt(adt, subs) if is_item(cx, adt.did, LangItem::OwnedBox) => subs.type_at(0).is_str(),
ty::Adt(adt, subs) => match is_any_item(cx, adt.did, &[sym::string_type, sym::Cow]) {
Some(0) => true,
Some(1) => subs.type_at(1).is_str(),
_ => false,
},
_ => false,
}
.then(|| RepeatKind::String)
}
}
Expand All @@ -52,7 +49,7 @@ pub(super) fn check(
) {
if_chain! {
if let ExprKind::Call(repeat_fn, [repeat_arg]) = take_self_arg.kind;
if is_item(cx, repeat_fn, &paths::ITER_REPEAT);
if is_item(cx, repeat_fn, sym::iter_repeat);
if is_item(cx, cx.typeck_results().expr_ty(collect_expr), sym::string_type);
if let Some(collect_id) = cx.typeck_results().type_dependent_def_id(collect_expr.hir_id);
if let Some(take_id) = cx.typeck_results().type_dependent_def_id(take_expr.hir_id);
Expand Down
7 changes: 4 additions & 3 deletions clippy_lints/src/methods/uninit_assumed_init.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use clippy_utils::diagnostics::span_lint;
use clippy_utils::{is_item, paths};
use clippy_utils::is_item;
use if_chain::if_chain;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_middle::ty::{self, Ty};
use rustc_span::sym;

use super::UNINIT_ASSUMED_INIT;

/// lint for `MaybeUninit::uninit().assume_init()` (we already have the latter)
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) {
if_chain! {
if let hir::ExprKind::Call(callee, []) = recv.kind;
if is_item(cx, callee, &paths::MEM_MAYBEUNINIT_UNINIT);
if is_item(cx, callee, sym::maybe_uninit_uninit);
if !is_maybe_uninit_ty_valid(cx, cx.typeck_results().expr_ty_adjusted(expr));
then {
span_lint(
Expand All @@ -28,7 +29,7 @@ fn is_maybe_uninit_ty_valid(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
match ty.kind() {
ty::Array(component, _) => is_maybe_uninit_ty_valid(cx, component),
ty::Tuple(types) => types.types().all(|ty| is_maybe_uninit_ty_valid(cx, ty)),
ty::Adt(adt, _) => is_item(cx, adt.did, &paths::MEM_MAYBEUNINIT),
ty::Adt(adt, _) => is_item(cx, adt.did, hir::LangItem::MaybeUninit),
_ => false,
}
}
Loading

0 comments on commit 3fc960d

Please sign in to comment.