Use diagnostic items for Into, IntoIterator, LinkedList, ptr::null, prt::null_mut

This commit is contained in:
xFrednet 2021-07-14 22:10:15 +02:00
parent 2ac21889bc
commit 6030428fd2
11 changed files with 45 additions and 26 deletions

View file

@ -1,11 +1,11 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::paths::INTO;
use clippy_utils::{match_def_path, meets_msrv, msrvs};
use clippy_utils::{meets_msrv, msrvs};
use if_chain::if_chain;
use rustc_hir as hir;
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_semver::RustcVersion;
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::symbol::sym;
declare_clippy_lint! {
/// **What it does:** Searches for implementations of the `Into<..>` trait and suggests to implement `From<..>` instead.
@ -62,7 +62,7 @@ impl LateLintPass<'_> for FromOverInto {
if_chain! {
if let hir::ItemKind::Impl{ .. } = &item.kind;
if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(item.def_id);
if match_def_path(cx, impl_trait_ref.def_id, &INTO);
if cx.tcx.is_diagnostic_item(sym::into_trait, impl_trait_ref.def_id);
then {
span_lint_and_help(

View file

@ -1,16 +1,17 @@
use super::EXPLICIT_INTO_ITER_LOOP;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_trait_method;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::{match_trait_method, paths};
use rustc_errors::Applicability;
use rustc_hir::Expr;
use rustc_lint::LateContext;
use rustc_middle::ty::TyS;
use rustc_span::symbol::sym;
pub(super) fn check(cx: &LateContext<'_>, self_arg: &'hir Expr<'hir>, call_expr: &Expr<'_>) {
let self_ty = cx.typeck_results().expr_ty(self_arg);
let self_ty_adjusted = cx.typeck_results().expr_ty_adjusted(self_arg);
if !(TyS::same_type(self_ty, self_ty_adjusted) && match_trait_method(cx, call_expr, &paths::INTO_ITERATOR)) {
if !(TyS::same_type(self_ty, self_ty_adjusted) && is_trait_method(cx, call_expr, sym::IntoIterator)) {
return;
}

View file

@ -1,8 +1,8 @@
use super::EXPLICIT_ITER_LOOP;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_trait_method;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::{is_type_diagnostic_item, match_type};
use clippy_utils::{match_trait_method, paths};
use clippy_utils::ty::is_type_diagnostic_item;
use rustc_errors::Applicability;
use rustc_hir::{Expr, Mutability};
use rustc_lint::LateContext;
@ -12,7 +12,7 @@ use rustc_span::sym;
pub(super) fn check(cx: &LateContext<'_>, self_arg: &Expr<'_>, arg: &Expr<'_>, method_name: &str) {
let should_lint = match method_name {
"iter" | "iter_mut" => is_ref_iterable_type(cx, self_arg),
"into_iter" if match_trait_method(cx, arg, &paths::INTO_ITERATOR) => {
"into_iter" if is_trait_method(cx, arg, sym::IntoIterator) => {
let receiver_ty = cx.typeck_results().expr_ty(self_arg);
let receiver_ty_adjusted = cx.typeck_results().expr_ty_adjusted(self_arg);
let ref_receiver_ty = cx.tcx.mk_ref(
@ -55,7 +55,7 @@ fn is_ref_iterable_type(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
let ty = cx.typeck_results().expr_ty(e);
is_iterable_array(ty, cx) ||
is_type_diagnostic_item(cx, ty, sym::vec_type) ||
match_type(cx, ty, &paths::LINKED_LIST) ||
is_type_diagnostic_item(cx, ty, sym::LinkedList) ||
is_type_diagnostic_item(cx, ty, sym::hashmap_type) ||
is_type_diagnostic_item(cx, ty, sym::hashset_type) ||
is_type_diagnostic_item(cx, ty, sym::vecdeque_type) ||

View file

@ -1794,7 +1794,7 @@ mod redundant_pattern_match {
|| is_type_diagnostic_item(cx, ty, sym::Arc)
|| is_type_diagnostic_item(cx, ty, sym::cstring_type)
|| is_type_diagnostic_item(cx, ty, sym::BTreeMap)
|| match_type(cx, ty, &paths::LINKED_LIST)
|| is_type_diagnostic_item(cx, ty, sym::LinkedList)
|| match_type(cx, ty, &paths::WEAK_RC)
|| match_type(cx, ty, &paths::WEAK_ARC)
{

View file

@ -1,8 +1,7 @@
use super::utils::derefs_to_slice;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::paths;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::{is_type_diagnostic_item, match_type};
use clippy_utils::ty::is_type_diagnostic_item;
use rustc_errors::Applicability;
use rustc_hir::Expr;
use rustc_lint::LateContext;
@ -26,7 +25,7 @@ pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, recv: &'tcx E
"BTreeMap"
} else if is_type_diagnostic_item(cx, ty, sym::BTreeSet) {
"BTreeSet"
} else if match_type(cx, ty, &paths::LINKED_LIST) {
} else if is_type_diagnostic_item(cx, ty, sym::LinkedList) {
"LinkedList"
} else if is_type_diagnostic_item(cx, ty, sym::BinaryHeap) {
"BinaryHeap"

View file

@ -4,7 +4,7 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_the
use clippy_utils::ptr::get_spans;
use clippy_utils::source::snippet_opt;
use clippy_utils::ty::{is_type_diagnostic_item, match_type, walk_ptrs_hir_ty};
use clippy_utils::{expr_path_res, is_lint_allowed, match_any_def_paths, paths};
use clippy_utils::{expr_path_res, is_lint_allowed, match_any_diagnostic_items, paths};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{
@ -419,7 +419,7 @@ fn get_rptr_lm<'tcx>(ty: &'tcx Ty<'tcx>) -> Option<(&'tcx Lifetime, Mutability,
fn is_null_path(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
if let ExprKind::Call(pathexp, []) = expr.kind {
expr_path_res(cx, pathexp).opt_def_id().map_or(false, |id| {
match_any_def_paths(cx, id, &[&paths::PTR_NULL, &paths::PTR_NULL_MUT]).is_some()
match_any_diagnostic_items(cx, id, &[sym::ptr_null, sym::ptr_null_mut]).is_some()
})
} else {
false

View file

@ -1,12 +1,13 @@
use clippy_utils::consts::{constant_context, Constant};
use clippy_utils::diagnostics::span_lint;
use clippy_utils::{is_expr_path_def_path, paths};
use clippy_utils::{is_expr_diagnostic_item, is_expr_path_def_path, paths};
use if_chain::if_chain;
use rustc_ast::LitKind;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::symbol::sym;
declare_clippy_lint! {
/// **What it does:** Checks for transmute calls which would receive a null pointer.
@ -67,7 +68,7 @@ impl<'tcx> LateLintPass<'tcx> for TransmutingNull {
// `std::mem::transmute(std::ptr::null::<i32>())`
if_chain! {
if let ExprKind::Call(func1, []) = arg.kind;
if is_expr_path_def_path(cx, func1, &paths::PTR_NULL);
if is_expr_diagnostic_item(cx, func1, sym::ptr_null);
then {
span_lint(cx, TRANSMUTING_NULL, expr.span, LINT_MSG)
}

View file

@ -1,12 +1,12 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::{match_def_path, paths};
use rustc_hir::{self as hir, def_id::DefId};
use rustc_lint::LateContext;
use rustc_span::symbol::sym;
use super::LINKEDLIST;
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, def_id: DefId) -> bool {
if match_def_path(cx, def_id, &paths::LINKED_LIST) {
if cx.tcx.is_diagnostic_item(sym::LinkedList, def_id) {
span_lint_and_help(
cx,
LINKEDLIST,

View file

@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
use clippy_utils::source::{snippet, snippet_with_macro_callsite};
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::{is_type_diagnostic_item, same_type_and_consts};
use clippy_utils::{get_parent_expr, match_def_path, match_trait_method, paths};
use clippy_utils::{get_parent_expr, is_trait_method, match_def_path, match_trait_method, paths};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, HirId, MatchSource};
@ -64,7 +64,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
},
ExprKind::MethodCall(name, .., args, _) => {
if match_trait_method(cx, e, &paths::INTO) && &*name.ident.as_str() == "into" {
if is_trait_method(cx, e, sym::into_trait) && &*name.ident.as_str() == "into" {
let a = cx.typeck_results().expr_ty(e);
let b = cx.typeck_results().expr_ty(&args[0]);
if same_type_and_consts(a, b) {
@ -80,7 +80,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
);
}
}
if match_trait_method(cx, e, &paths::INTO_ITERATOR) && name.ident.name == sym::into_iter {
if is_trait_method(cx, e, sym::IntoIterator) && name.ident.name == sym::into_iter {
if let Some(parent_expr) = get_parent_expr(cx, e) {
if let ExprKind::MethodCall(parent_name, ..) = parent_expr.kind {
if parent_name.ident.name != sym::into_iter {