Skip to content

Commit

Permalink
implementation using new diagnostic items
Browse files Browse the repository at this point in the history
  • Loading branch information
FoseFx committed Mar 26, 2022
1 parent f624bf9 commit d418857
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions clippy_lints/src/strings.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg};
use clippy_utils::source::{snippet, snippet_with_applicability};
use clippy_utils::ty::{get_associated_type, implements_trait, is_type_diagnostic_item};
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{get_parent_expr, is_lint_allowed, match_function_call, method_calls, paths};
use clippy_utils::{peel_blocks, SpanlessEq};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::def_id::DefId;
use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, LangItem, QPath};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::{self, Ty};
use rustc_middle::ty;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Spanned;
use rustc_span::sym;
Expand Down Expand Up @@ -476,13 +477,16 @@ declare_lint_pass!(TrimSplitWhitespace => [TRIM_SPLIT_WHITESPACE]);

impl<'tcx> LateLintPass<'tcx> for TrimSplitWhitespace {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
let tyckres = cx.typeck_results();
if_chain! {
if let ExprKind::MethodCall(path, [recv], split_ws_span) = expr.kind;
if let ExprKind::MethodCall(path, [split_recv], split_ws_span) = expr.kind;
if path.ident.name == sym!(split_whitespace);
if let ExprKind::MethodCall(path, [recv], trim_span) = recv.kind;
if let Some(split_ws_def_id) = tyckres.type_dependent_def_id(expr.hir_id);
if cx.tcx.is_diagnostic_item(sym::str_split_whitespace, split_ws_def_id);
if let ExprKind::MethodCall(path, [_trim_recv], trim_span) = split_recv.kind;
if let trim_fn_name @ ("trim" | "trim_start" | "trim_end") = path.ident.name.as_str();
let recv_ty = cx.typeck_results().expr_ty(recv).peel_refs();
if recv_ty.is_str() || implements_deref_str(cx, recv_ty);
if let Some(trim_def_id) = tyckres.type_dependent_def_id(split_recv.hir_id);
if is_one_of_trim_diagnostic_items(cx, trim_def_id);
then {
span_lint_and_sugg(
cx,
Expand All @@ -498,17 +502,8 @@ impl<'tcx> LateLintPass<'tcx> for TrimSplitWhitespace {
}
}

/// does ty implement Deref<Target=str>?
fn implements_deref_str<'t>(cx: &LateContext<'t>, ty: Ty<'t>) -> bool {
if_chain! {
if let Some(deref_trait_id) = cx.tcx.get_diagnostic_item(sym::Deref);
if implements_trait(cx, ty, deref_trait_id, &[]);
if let Some(target) = get_associated_type(cx, ty, deref_trait_id, "Target");
then {
target.is_str()
}
else {
false
}
}
fn is_one_of_trim_diagnostic_items(cx: &LateContext<'_>, trim_def_id: DefId) -> bool {
cx.tcx.is_diagnostic_item(sym::str_trim, trim_def_id)
|| cx.tcx.is_diagnostic_item(sym::str_trim_start, trim_def_id)
|| cx.tcx.is_diagnostic_item(sym::str_trim_end, trim_def_id)
}

0 comments on commit d418857

Please sign in to comment.