Skip to content

Commit 38bcd8e

Browse files
committed
use math_def_path when check method name
1 parent 41c8501 commit 38bcd8e

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

clippy_lints/src/bytes_count_to_len.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::ty::is_type_diagnostic_item;
4+
use clippy_utils::{match_def_path, paths};
45
use if_chain::if_chain;
56
use rustc_errors::Applicability;
67
use rustc_hir as hir;
@@ -39,12 +40,14 @@ declare_lint_pass!(BytesCountToLen => [BYTES_COUNT_TO_LEN]);
3940
impl<'tcx> LateLintPass<'tcx> for BytesCountToLen {
4041
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
4142
if_chain! {
42-
if let hir::ExprKind::MethodCall(expr_method, expr_args, _) = &expr.kind;
43-
if expr_method.ident.name == sym::count;
43+
if let hir::ExprKind::MethodCall(_, expr_args, _) = &expr.kind;
44+
if let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
45+
if match_def_path(cx, expr_def_id, &paths::ITER_COUNT);
4446

4547
if let [bytes_expr] = &**expr_args;
46-
if let hir::ExprKind::MethodCall(bytes_method, bytes_args, _) = &bytes_expr.kind;
47-
if bytes_method.ident.name.as_str() == "bytes";
48+
if let hir::ExprKind::MethodCall(_, bytes_args, _) = &bytes_expr.kind;
49+
if let Some(bytes_def_id) = cx.typeck_results().type_dependent_def_id(bytes_expr.hir_id);
50+
if match_def_path(cx, bytes_def_id, &paths::STR_BYTES);
4851

4952
if let [str_expr] = &**bytes_args;
5053
let ty = cx.typeck_results().expr_ty(str_expr).peel_refs();

clippy_utils/src/paths.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub const IO_READ: [&str; 3] = ["std", "io", "Read"];
6161
pub const IO_WRITE: [&str; 3] = ["std", "io", "Write"];
6262
pub const IPADDR_V4: [&str; 5] = ["std", "net", "ip", "IpAddr", "V4"];
6363
pub const IPADDR_V6: [&str; 5] = ["std", "net", "ip", "IpAddr", "V6"];
64+
pub const ITER_COUNT: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "count"];
6465
pub const ITER_REPEAT: [&str; 5] = ["core", "iter", "sources", "repeat", "repeat"];
6566
#[allow(clippy::invalid_paths)] // internal lints do not know about all external crates
6667
pub const ITERTOOLS_NEXT_TUPLE: [&str; 3] = ["itertools", "Itertools", "next_tuple"];
@@ -149,6 +150,7 @@ pub const STD_FS_CREATE_DIR: [&str; 3] = ["std", "fs", "create_dir"];
149150
pub const STRING_AS_MUT_STR: [&str; 4] = ["alloc", "string", "String", "as_mut_str"];
150151
pub const STRING_AS_STR: [&str; 4] = ["alloc", "string", "String", "as_str"];
151152
pub const STRING_NEW: [&str; 4] = ["alloc", "string", "String", "new"];
153+
pub const STR_BYTES: [&str; 4] = ["core", "str", "<impl str>", "bytes"];
152154
pub const STR_ENDS_WITH: [&str; 4] = ["core", "str", "<impl str>", "ends_with"];
153155
pub const STR_FROM_UTF8: [&str; 4] = ["core", "str", "converts", "from_utf8"];
154156
pub const STR_LEN: [&str; 4] = ["core", "str", "<impl str>", "len"];

0 commit comments

Comments
 (0)