Skip to content

Commit f027fc8

Browse files
authored
Rollup merge of rust-lang#142787 - samueltardieu:diag-items-for-clippy, r=Manishearth,Urgau
Add diagnostic items for Clippy Clippy still uses some paths to access items from the standard library. Adding the missing diagnostic items allows removing the last remaining paths. Closes rust-lang/rust-clippy#5393
2 parents f53cd2b + 65402ab commit f027fc8

File tree

7 files changed

+16
-24
lines changed

7 files changed

+16
-24
lines changed

clippy_lints/src/casts/manual_dangling_ptr.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::SpanRangeExt;
3-
use clippy_utils::{expr_or_init, path_def_id, paths, std_or_core};
3+
use clippy_utils::{expr_or_init, is_path_diagnostic_item, std_or_core, sym};
44
use rustc_ast::LitKind;
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, ExprKind, GenericArg, Mutability, QPath, Ty, TyKind};
@@ -53,8 +53,7 @@ fn is_expr_const_aligned(cx: &LateContext<'_>, expr: &Expr<'_>, to: &Ty<'_>) ->
5353

5454
fn is_align_of_call(cx: &LateContext<'_>, fun: &Expr<'_>, to: &Ty<'_>) -> bool {
5555
if let ExprKind::Path(QPath::Resolved(_, path)) = fun.kind
56-
&& let Some(fun_id) = path_def_id(cx, fun)
57-
&& paths::ALIGN_OF.matches(cx, fun_id)
56+
&& is_path_diagnostic_item(cx, fun, sym::mem_align_of)
5857
&& let Some(args) = path.segments.last().and_then(|seg| seg.args)
5958
&& let [GenericArg::Type(generic_ty)] = args.args
6059
{

clippy_lints/src/manual_option_as_slice.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
33
use clippy_utils::msrvs::Msrv;
4-
use clippy_utils::{is_none_arm, msrvs, paths, peel_hir_expr_refs, sym};
4+
use clippy_utils::{is_none_arm, msrvs, peel_hir_expr_refs, sym};
55
use rustc_errors::Applicability;
66
use rustc_hir::def::{DefKind, Res};
77
use rustc_hir::{Arm, Expr, ExprKind, LangItem, Pat, PatKind, QPath, is_range_literal};
@@ -220,5 +220,5 @@ fn is_empty_slice(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
220220
}
221221

222222
fn is_slice_from_ref(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
223-
paths::SLICE_FROM_REF.matches_path(cx, expr)
223+
clippy_utils::is_path_diagnostic_item(cx, expr, sym::slice_from_ref)
224224
}

clippy_lints/src/methods/io_other_error.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::msrvs::{self, Msrv};
3-
use clippy_utils::{expr_or_init, paths};
3+
use clippy_utils::{expr_or_init, is_path_diagnostic_item, sym};
44
use rustc_errors::Applicability;
55
use rustc_hir::{Expr, ExprKind, QPath};
66
use rustc_lint::LateContext;
@@ -10,8 +10,11 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, path: &Expr<'_>, args
1010
&& !expr.span.from_expansion()
1111
&& !error_kind.span.from_expansion()
1212
&& let ExprKind::Path(QPath::TypeRelative(_, new_segment)) = path.kind
13-
&& paths::IO_ERROR_NEW.matches_path(cx, path)
14-
&& paths::IO_ERRORKIND_OTHER_CTOR.matches_path(cx, expr_or_init(cx, error_kind))
13+
&& is_path_diagnostic_item(cx, path, sym::io_error_new)
14+
&& let ExprKind::Path(QPath::Resolved(_, init_path)) = &expr_or_init(cx, error_kind).kind
15+
&& let [.., error_kind_ty, error_kind_variant] = init_path.segments
16+
&& cx.tcx.is_diagnostic_item(sym::io_errorkind, error_kind_ty.res.def_id())
17+
&& error_kind_variant.ident.name == sym::Other
1518
&& msrv.meets(cx, msrvs::IO_ERROR_OTHER)
1619
{
1720
span_lint_and_then(

clippy_lints/src/single_range_in_vec_init.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::higher::VecArgs;
33
use clippy_utils::macros::root_macro_call_first_node;
44
use clippy_utils::source::SpanRangeExt;
55
use clippy_utils::ty::implements_trait;
6-
use clippy_utils::{is_no_std_crate, paths};
6+
use clippy_utils::{is_no_std_crate, sym};
77
use rustc_ast::{LitIntType, LitKind, UintTy};
88
use rustc_errors::Applicability;
99
use rustc_hir::{Expr, ExprKind, LangItem, QPath, StructTailExpr};
@@ -100,7 +100,7 @@ impl LateLintPass<'_> for SingleRangeInVecInit {
100100
&& let Some(start_snippet) = start.span.get_source_text(cx)
101101
&& let Some(end_snippet) = end.span.get_source_text(cx)
102102
{
103-
let should_emit_every_value = if let Some(step_def_id) = paths::ITER_STEP.only(cx)
103+
let should_emit_every_value = if let Some(step_def_id) = cx.tcx.get_diagnostic_item(sym::range_step)
104104
&& implements_trait(cx, ty, step_def_id, &[])
105105
{
106106
true

clippy_lints/src/to_digit_is_some.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::msrvs::{self, Msrv};
44
use clippy_utils::source::snippet_with_applicability;
5-
use clippy_utils::{is_in_const_context, paths, sym};
5+
use clippy_utils::{is_in_const_context, is_path_diagnostic_item, sym};
66
use rustc_errors::Applicability;
77
use rustc_hir as hir;
88
use rustc_lint::{LateContext, LateLintPass};
@@ -62,7 +62,7 @@ impl<'tcx> LateLintPass<'tcx> for ToDigitIsSome {
6262
}
6363
},
6464
hir::ExprKind::Call(to_digits_call, [char_arg, radix_arg]) => {
65-
if paths::CHAR_TO_DIGIT.matches_path(cx, to_digits_call) {
65+
if is_path_diagnostic_item(cx, to_digits_call, sym::char_to_digit) {
6666
Some((false, char_arg, radix_arg))
6767
} else {
6868
None

clippy_lints/src/useless_concat.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::macros::macro_backtrace;
3-
use clippy_utils::paths::CONCAT;
43
use clippy_utils::source::snippet_opt;
5-
use clippy_utils::tokenize_with_text;
4+
use clippy_utils::{sym, tokenize_with_text};
65
use rustc_ast::LitKind;
76
use rustc_errors::Applicability;
87
use rustc_hir::{Expr, ExprKind};
@@ -43,7 +42,7 @@ impl LateLintPass<'_> for UselessConcat {
4342
// Get the direct parent of the expression.
4443
&& let Some(macro_call) = macro_backtrace(expr.span).next()
4544
// Check if the `concat` macro from the `core` library.
46-
&& CONCAT.matches(cx, macro_call.def_id)
45+
&& cx.tcx.is_diagnostic_item(sym::macro_concat, macro_call.def_id)
4746
// We get the original code to parse it.
4847
&& let Some(original_code) = snippet_opt(cx, macro_call.span)
4948
// This check allows us to ensure that the code snippet:

clippy_utils/src/paths.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,6 @@ path_macros! {
126126
macro_path: PathNS::Macro,
127127
}
128128

129-
// Paths in `core`/`alloc`/`std`. This should be avoided and cleaned up by adding diagnostic items.
130-
pub static ALIGN_OF: PathLookup = value_path!(core::mem::align_of);
131-
pub static CHAR_TO_DIGIT: PathLookup = value_path!(char::to_digit);
132-
pub static CONCAT: PathLookup = macro_path!(core::concat);
133-
pub static IO_ERROR_NEW: PathLookup = value_path!(std::io::Error::new);
134-
pub static IO_ERRORKIND_OTHER_CTOR: PathLookup = value_path!(std::io::ErrorKind::Other);
135-
pub static ITER_STEP: PathLookup = type_path!(core::iter::Step);
136-
pub static SLICE_FROM_REF: PathLookup = value_path!(core::slice::from_ref);
137-
138129
// Paths in external crates
139130
pub static FUTURES_IO_ASYNCREADEXT: PathLookup = type_path!(futures_util::AsyncReadExt);
140131
pub static FUTURES_IO_ASYNCWRITEEXT: PathLookup = type_path!(futures_util::AsyncWriteExt);

0 commit comments

Comments
 (0)