Skip to content

Commit 6ce6b29

Browse files
committed
Use diagnostic items for intrinsics::transmute, TryInto
1 parent 6030428 commit 6ce6b29

File tree

4 files changed

+9
-8
lines changed

4 files changed

+9
-8
lines changed

clippy_lints/src/transmute/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ mod useless_transmute;
1212
mod utils;
1313
mod wrong_transmute;
1414

15-
use clippy_utils::{in_constant, match_def_path, paths};
15+
use clippy_utils::in_constant;
1616
use if_chain::if_chain;
1717
use rustc_hir::{Expr, ExprKind};
1818
use rustc_lint::{LateContext, LateLintPass};
1919
use rustc_session::{declare_lint_pass, declare_tool_lint};
20+
use rustc_span::symbol::sym;
2021

2122
declare_clippy_lint! {
2223
/// **What it does:** Checks for transmutes that can't ever be correct on any
@@ -328,7 +329,7 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
328329
if let ExprKind::Call(path_expr, args) = e.kind;
329330
if let ExprKind::Path(ref qpath) = path_expr.kind;
330331
if let Some(def_id) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id();
331-
if match_def_path(cx, def_id, &paths::TRANSMUTE);
332+
if cx.tcx.is_diagnostic_item(sym::transmute, def_id);
332333
then {
333334
// Avoid suggesting from/to bits and dereferencing raw pointers in const contexts.
334335
// See https://github.com/rust-lang/rust/issues/73736 for progress on making them `const fn`.

clippy_lints/src/transmuting_null.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::consts::{constant_context, Constant};
22
use clippy_utils::diagnostics::span_lint;
3-
use clippy_utils::{is_expr_diagnostic_item, is_expr_path_def_path, paths};
3+
use clippy_utils::is_expr_diagnostic_item;
44
use if_chain::if_chain;
55
use rustc_ast::LitKind;
66
use rustc_hir::{Expr, ExprKind};
@@ -39,7 +39,7 @@ impl<'tcx> LateLintPass<'tcx> for TransmutingNull {
3939

4040
if_chain! {
4141
if let ExprKind::Call(func, [arg]) = expr.kind;
42-
if is_expr_path_def_path(cx, func, &paths::TRANSMUTE);
42+
if is_expr_diagnostic_item(cx, func, sym::transmute);
4343

4444
then {
4545
// Catching transmute over constants that resolve to `null`.

clippy_lints/src/useless_conversion.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
22
use clippy_utils::source::{snippet, snippet_with_macro_callsite};
33
use clippy_utils::sugg::Sugg;
44
use clippy_utils::ty::{is_type_diagnostic_item, same_type_and_consts};
5-
use clippy_utils::{get_parent_expr, is_trait_method, match_def_path, match_trait_method, paths};
5+
use clippy_utils::{get_parent_expr, is_trait_method, match_def_path, paths};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir::{Expr, ExprKind, HirId, MatchSource};
@@ -104,7 +104,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
104104
}
105105
}
106106
if_chain! {
107-
if match_trait_method(cx, e, &paths::TRY_INTO_TRAIT) && name.ident.name == sym::try_into;
107+
if is_trait_method(cx, e, sym::try_into_trait) && name.ident.name == sym::try_into;
108108
let a = cx.typeck_results().expr_ty(e);
109109
let b = cx.typeck_results().expr_ty(&args[0]);
110110
if is_type_diagnostic_item(cx, a, sym::result_type);

clippy_utils/src/paths.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ pub const REGEX_BYTES_NEW: [&str; 4] = ["regex", "re_bytes", "Regex", "new"];
145145
pub const REGEX_BYTES_SET_NEW: [&str; 5] = ["regex", "re_set", "bytes", "RegexSet", "new"];
146146
pub const REGEX_NEW: [&str; 4] = ["regex", "re_unicode", "Regex", "new"];
147147
pub const REGEX_SET_NEW: [&str; 5] = ["regex", "re_set", "unicode", "RegexSet", "new"];
148+
/// Preferably use the diagnostic item `sym::result_type` where possible
148149
pub const RESULT: [&str; 3] = ["core", "result", "Result"];
149150
pub const RESULT_ERR: [&str; 4] = ["core", "result", "Result", "Err"];
150151
pub const RESULT_OK: [&str; 4] = ["core", "result", "Result", "Ok"];
@@ -180,9 +181,8 @@ pub const SYM_MODULE: [&str; 3] = ["rustc_span", "symbol", "sym"];
180181
pub const SYNTAX_CONTEXT: [&str; 3] = ["rustc_span", "hygiene", "SyntaxContext"];
181182
pub const TO_OWNED_METHOD: [&str; 4] = ["alloc", "borrow", "ToOwned", "to_owned"];
182183
pub const TO_STRING_METHOD: [&str; 4] = ["alloc", "string", "ToString", "to_string"];
183-
pub const TRANSMUTE: [&str; 4] = ["core", "intrinsics", "", "transmute"];
184184
pub const TRY_FROM: [&str; 4] = ["core", "convert", "TryFrom", "try_from"];
185-
pub const TRY_INTO_TRAIT: [&str; 3] = ["core", "convert", "TryInto"];
185+
186186
pub const VEC: [&str; 3] = ["alloc", "vec", "Vec"];
187187
pub const VEC_AS_MUT_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_mut_slice"];
188188
pub const VEC_AS_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_slice"];

0 commit comments

Comments
 (0)