Skip to content

Update Clippy #103337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 116 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
116 commits
Select commit Hold shift + click to select a range
0958f94
Add `manual_filter` lint for `Option`
kraktus Oct 3, 2022
b91dc03
Add `as_ptr_cast_mut` lint
Noratrieb Oct 1, 2022
169ef78
Improve wording
Noratrieb Oct 1, 2022
2b944d0
Fix example
Noratrieb Oct 1, 2022
b89ac0c
refactor `manual_filter`
kraktus Oct 3, 2022
830fdf2
update rust version introduction
kraktus Oct 2, 2022
9e70a0f
Replace if_chain with let chains in `clippy::author` output
Alexendoo Oct 5, 2022
d75b25f
Merge commit 'ac0e10aa68325235069a842f47499852b2dee79e' into clippyup
flip1995 Oct 6, 2022
13dbc33
Remove `mir::CastKind::Misc`
ouz-a Oct 4, 2022
d3c041a
extend `box-default` lint, add suggestion
llogiq Oct 3, 2022
09a554d
Merge commit '8f1ebdd18bdecc621f16baaf779898cc08cc2766' into clippyup
flip1995 Oct 6, 2022
4936848
Change uninlined_format_args into a style lint
nyurik Oct 6, 2022
23b1699
Add curlies to scrutinees with side effects
evantypanski Oct 6, 2022
39164ac
Fix flipped variable that made it through
evantypanski Oct 6, 2022
3690199
Auto merge of #9601 - evantypanski:et/issue9575, r=Manishearth
bors Oct 6, 2022
65ae666
Auto merge of #9595 - Alexendoo:author-let-chains, r=Jarcho
bors Oct 7, 2022
037f698
`needless_borrow` uses `used_exactly_once`
smoelius Aug 25, 2022
9cc8da2
Fix adjacent code
smoelius Oct 7, 2022
3b328e7
Introduce TypeErrCtxt
camsteffen Sep 9, 2022
6819e85
Change InferCtxtBuilder from enter to build
camsteffen Sep 20, 2022
2c8e473
Auto merge of #9585 - rust-lang:extend-box-default, r=Alexendoo
bors Oct 7, 2022
e91746e
make const_err a hard error
RalfJung Sep 21, 2022
7c372cd
Auto merge of #102091 - RalfJung:const_err, r=oli-obk
bors Oct 7, 2022
5f6e1d3
Stabilize half_open_range_patterns
Urgau Sep 24, 2022
4013d36
Rollup merge of #102675 - ouz-a:mir-technical-debt, r=oli-obk
matthiaskrgr Oct 8, 2022
6f4546a
[`unnecessary_cast`] Do not lint negative hexadecimal literals when c…
kraktus Oct 8, 2022
39a7d00
Don't suggest moving tuple structs with a significant drop to late ev…
Jarcho Oct 8, 2022
292e313
Auto merge of #9451 - kraktus:manual_filter2, r=dswij
bors Oct 8, 2022
272bbfb
Auto merge of #9386 - smoelius:further-enhance-needless-borrow, r=Jarcho
bors Oct 8, 2022
8e76d66
ImplItemKind::TyAlias => ImplItemKind::Type
compiler-errors Oct 9, 2022
3fc903e
Fix clippy tests that trigger `for_loop_over_fallibles` lint
WaffleLapkin Aug 25, 2022
05dcfd9
fixup lint name
WaffleLapkin Oct 7, 2022
7cfc6fa
deprecate `clippy::for_loops_over_fallibles`
WaffleLapkin Oct 7, 2022
1688368
feat: add Default to Lint groups
unvalley Oct 9, 2022
5ccf727
Rollup merge of #102829 - compiler-errors:rename-impl-item-kind, r=Ta…
JohnTitor Oct 9, 2022
e7a5249
Rollup merge of #102275 - Urgau:stabilize-half_open_range_patterns, r…
JohnTitor Oct 10, 2022
7a42219
Rename AssocItemKind::TyAlias to AssocItemKind::Type
compiler-errors Oct 10, 2022
5577e42
Rollup merge of #99696 - WaffleLapkin:uplift, r=fee1-dead
Dylan-DPC Oct 10, 2022
1469e8d
Rollup merge of #102868 - compiler-errors:rename-assoc-tyalias-to-ty,…
Dylan-DPC Oct 10, 2022
178799f
fix: change command order and remove waste condition
unvalley Oct 10, 2022
cf72565
Auto merge of #9610 - Jarcho:fix-9608, r=Alexendoo
bors Oct 10, 2022
c3f077c
Fix unclosed HTML tag in clippy doc
GuillaumeGomez Oct 10, 2022
bd61fdb
fix `box-default` ignoring trait objects' types
llogiq Oct 10, 2022
0cc7492
Use the correct type when comparing nested constants.
Jarcho Oct 10, 2022
f48d13f
Replace manual let else patterns with let else
est31 Oct 10, 2022
8e87d39
Auto merge of #9572 - Nilstrieb:as-ptr-cast-mut, r=dswij
bors Oct 11, 2022
122ae22
Auto merge of #9616 - unvalley:add-default-to-lint-groups, r=xFrednet
bors Oct 11, 2022
854015c
Auto merge of #9629 - est31:let_else, r=Jarcho
bors Oct 11, 2022
74ba7e1
Handle panic! inline_format-arg before ed2021
nyurik Oct 8, 2022
ac6d2ba
add tests in `implicit_saturating_sub` lint
roynrishingha Oct 3, 2022
2d58817
Auto merge of #9605 - nyurik:fix-inline-edition, r=llogiq
bors Oct 12, 2022
31006b4
update Applicability of `rc_buffer` from `MachineApplicable` to
roynrishingha Oct 12, 2022
e4c80f2
add `cast-nan-to-int` lint
llogiq Oct 8, 2022
6354d12
Auto merge of #9633 - royrustdev:fix_rc_buffer, r=llogiq
bors Oct 12, 2022
b8a9a50
Auto merge of #9617 - llogiq:cast-nan-to-int, r=Alexendoo
bors Oct 12, 2022
524ec2e
Fix bug in `referent_used_exactly_once`
smoelius Oct 11, 2022
45dd9f3
Auto merge of #9627 - Jarcho:ice-9625, r=xFrednet
bors Oct 12, 2022
e51e930
`default_numeric_fallback` do not lint on constants
kraktus Oct 12, 2022
36b2685
refactor `default_numeric_fallback`
kraktus Oct 12, 2022
7717904
Fix to_string_in_format_args in parens
nyurik Oct 4, 2022
42bdfa2
Auto merge of #9590 - nyurik:fix-parens, r=Alexendoo
bors Oct 12, 2022
124caeb
Fix edition revision ui tests
Alexendoo Oct 13, 2022
58ef56e
Auto merge of #9600 - nyurik:inline-fmt-style, r=llogiq
bors Oct 13, 2022
fe3200c
Auto merge of #9584 - royrustdev:implicit_saturating_sub, r=llogiq
bors Oct 13, 2022
4e89ffa
Auto merge of #9640 - Alexendoo:edition-revisions, r=llogiq
bors Oct 13, 2022
a31462a
Enable cargo sparse registry in CI
Alexendoo Oct 13, 2022
9a6eca5
Auto merge of #9641 - Alexendoo:sparse-registry, r=flip1995
bors Oct 13, 2022
135a273
Book: Small grammar + link a11y change
Oct 14, 2022
4f50e6f
Remove CastCheckResult since it's unused
compiler-errors Sep 14, 2022
950f5fa
add missing comma
hkBst Oct 14, 2022
8e0da62
Auto merge of #9644 - hkBst:patch-1, r=flip1995
bors Oct 14, 2022
efa361b
Auto merge of #9643 - icecream17:patch-1, r=flip1995
bors Oct 14, 2022
344b7bc
Don't lint `ptr_arg` when used as an incompatible trait object
Jarcho Oct 14, 2022
b510557
Auto merge of #9645 - Jarcho:ptr_arg_9542, r=llogiq
bors Oct 14, 2022
4b8df8d
Add a suggestion and a note about orphan rules for `from_over_into`
Alexendoo Oct 14, 2022
50f192f
Auto merge of #9649 - Alexendoo:from-over-into-suggestion, r=llogiq
bors Oct 15, 2022
dfd3525
Separate internal lints by pass
smoelius Oct 1, 2022
c84ac4c
Move some things around
smoelius Oct 1, 2022
8611a0b
Expand `unnecessary_def_path` lint
smoelius Oct 1, 2022
2e5e356
Fix adjacent code
smoelius Oct 1, 2022
5dc54c6
Format affected files
smoelius Oct 9, 2022
d38175f
`explicit_ty_bound` code golf
kraktus Oct 15, 2022
2e3342a
[`zero_prefixed_literal`] Do not advise to use octal form if not poss…
kraktus Oct 15, 2022
f8ae2f5
fix `box-default` linting `no_std` non-boxes
llogiq Oct 15, 2022
ff33d6e
Auto merge of #9655 - llogiq:unbox-default, r=dswij
bors Oct 16, 2022
eba5ff9
Auto merge of #9636 - kraktus:numeric-fallback, r=dswij
bors Oct 16, 2022
332b5b3
Auto merge of #9566 - smoelius:diagnostic-item-path, r=dswij
bors Oct 16, 2022
7ac97b6
Add new lint `partial_pub_fields`
TennyZhuang Oct 16, 2022
b10882a
fix dogfood
TennyZhuang Oct 16, 2022
abd5db3
add many tests
TennyZhuang Oct 16, 2022
360b48b
fix a doctest
TennyZhuang Oct 16, 2022
d917590
Auto merge of #9658 - TennyZhuang:partial-pub-fields, r=llogiq
bors Oct 16, 2022
34142fd
Auto merge of #9652 - kraktus:octo_89, r=xFrednet
bors Oct 16, 2022
4eaf543
Auto merge of #9609 - kraktus:hexa_f32, r=giraffate
bors Oct 17, 2022
136c2cd
Add `unused_format_specs` lint
Alexendoo Oct 13, 2022
502e87c
Auto merge of #9637 - Alexendoo:unused-format-specs, r=xFrednet
bors Oct 17, 2022
1da1ff6
Update Applicability of `redundant_allocation` lint from
roynrishingha Oct 12, 2022
1afc7e2
Auto merge of #9634 - royrustdev:fix_redundant_allocation, r=Manishearth
bors Oct 19, 2022
b6a860e
Add `missing_trait_methods` lint
Alexendoo Oct 17, 2022
fb8ecb9
Merge remote-tracking branch 'upstream/master' into rustup
flip1995 Oct 20, 2022
4ff2364
Bump nightly version -> 2022-10-20
flip1995 Oct 20, 2022
a4e8726
Auto merge of #9680 - flip1995:rustup, r=flip1995
bors Oct 20, 2022
4612fdf
Auto merge of #9670 - Alexendoo:missing-trait-methods, r=Jarcho
bors Oct 20, 2022
967f172
Auto merge of #9635 - smoelius:fix-9386-bug, r=Jarcho
bors Oct 20, 2022
615b761
`ref_option_ref` do not lint when inner reference is mutable
kraktus Oct 21, 2022
487c6fc
[`collapsible_match`] specify field name when destructuring structs
kraktus Oct 21, 2022
b2e5a71
Auto merge of #9685 - kraktus:collapsible_match, r=llogiq
bors Oct 21, 2022
b72e451
Auto merge of #9684 - kraktus:ref_option_ref, r=xFrednet
bors Oct 21, 2022
815876d
Move MSRV tests into the lint specific test files
Alexendoo Oct 21, 2022
5c6c353
Add `lintcheck` to packages linted by `dogfood` test
smoelius Oct 22, 2022
e38bb1a
Apply `--fix` fixes
smoelius Oct 22, 2022
bbee1c9
Apply manual fixes
smoelius Oct 22, 2022
b62ef60
Auto merge of #9691 - smoelius:lint-lintcheck, r=llogiq
bors Oct 22, 2022
28cd1ec
Auto merge of #9688 - Alexendoo:msrv-tests, r=Manishearth
bors Oct 23, 2022
4f142aa
Auto merge of #9622 - llogiq:box-dyn-default, r=Alexendoo
bors Oct 23, 2022
da9755b
Merge commit '4f142aa1058f14f153f8bfd2d82f04ddb9982388' into clippyup
flip1995 Oct 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Expand unnecessary_def_path lint
  • Loading branch information
smoelius committed Oct 15, 2022
commit 8611a0bb5cc401f90162449eaa82295ef5f70d68
2 changes: 1 addition & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|_| {
Box::<utils::internal_lints::lint_without_lint_pass::LintWithoutLintPass>::default()
});
store.register_late_pass(|_| Box::new(utils::internal_lints::unnecessary_def_path::UnnecessaryDefPath));
store.register_late_pass(|_| Box::<utils::internal_lints::unnecessary_def_path::UnnecessaryDefPath>::default());
store.register_late_pass(|_| Box::new(utils::internal_lints::outer_expn_data_pass::OuterExpnDataPass));
store.register_late_pass(|_| Box::new(utils::internal_lints::msrv_attr_impl::MsrvAttrImpl));
}
Expand Down
181 changes: 132 additions & 49 deletions clippy_lints/src/utils/internal_lints/unnecessary_def_path.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_then};
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::{def_path_res, is_lint_allowed, match_any_def_paths, peel_hir_expr_refs};
use if_chain::if_chain;
use rustc_ast::ast::LitKind;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Namespace, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::{ExprKind, Local, Mutability, Node};
use rustc_hir::{Expr, ExprKind, Local, Mutability, Node};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::mir::interpret::{Allocation, ConstValue, GlobalAlloc};
use rustc_middle::ty::{self, AssocKind, DefIdTree, Ty};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::symbol::{Ident, Symbol};
use rustc_span::Span;

use std::str;

Expand All @@ -38,11 +40,56 @@ declare_clippy_lint! {
"using a def path when a diagnostic item or a `LangItem` is available"
}

declare_lint_pass!(UnnecessaryDefPath => [UNNECESSARY_DEF_PATH]);
impl_lint_pass!(UnnecessaryDefPath => [UNNECESSARY_DEF_PATH]);

#[derive(Default)]
pub struct UnnecessaryDefPath {
array_def_ids: FxHashSet<(DefId, Span)>,
linted_def_ids: FxHashSet<DefId>,
}

#[allow(clippy::too_many_lines)]
impl<'tcx> LateLintPass<'tcx> for UnnecessaryDefPath {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
if is_lint_allowed(cx, UNNECESSARY_DEF_PATH, expr.hir_id) {
return;
}

match expr.kind {
ExprKind::Call(func, args) => self.check_call(cx, func, args, expr.span),
ExprKind::Array(elements) => self.check_array(cx, elements, expr.span),
_ => {},
}
}

fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
for &(def_id, span) in &self.array_def_ids {
if self.linted_def_ids.contains(&def_id) {
continue;
}

let (msg, sugg) = if let Some(sym) = cx.tcx.get_diagnostic_name(def_id) {
("diagnostic item", format!("sym::{sym}"))
} else if let Some(sym) = get_lang_item_name(cx, def_id) {
("language item", format!("LangItem::{sym}"))
} else {
continue;
};

span_lint_and_help(
cx,
UNNECESSARY_DEF_PATH,
span,
&format!("hardcoded path to a {msg}"),
None,
&format!("convert all references to use `{sugg}`"),
);
}
}
}

impl UnnecessaryDefPath {
#[allow(clippy::too_many_lines)]
fn check_call(&mut self, cx: &LateContext<'_>, func: &Expr<'_>, args: &[Expr<'_>], span: Span) {
enum Item {
LangItem(Symbol),
DiagnosticItem(Symbol),
Expand All @@ -54,42 +101,17 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryDefPath {
&["clippy_utils", "is_expr_path_def_path"],
];

if is_lint_allowed(cx, UNNECESSARY_DEF_PATH, expr.hir_id) {
return;
}

if_chain! {
if let ExprKind::Call(func, [cx_arg, def_arg, args@..]) = expr.kind;
if let [cx_arg, def_arg, args@..] = args;
if let ExprKind::Path(path) = &func.kind;
if let Some(id) = cx.qpath_res(path, func.hir_id).opt_def_id();
if let Some(which_path) = match_any_def_paths(cx, id, PATHS);
let item_arg = if which_path == 4 { &args[1] } else { &args[0] };
// Extract the path to the matched type
if let Some(segments) = path_to_matched_type(cx, item_arg);
let segments: Vec<&str> = segments.iter().map(|sym| &**sym).collect();
if let Some(def_id) = def_path_res(cx, &segments[..], None).opt_def_id();
if let Some(def_id) = inherent_def_path_res(cx, &segments[..]);
then {
// def_path_res will match field names before anything else, but for this we want to match
// inherent functions first.
let def_id = if cx.tcx.def_kind(def_id) == DefKind::Field {
let method_name = *segments.last().unwrap();
cx.tcx.def_key(def_id).parent
.and_then(|parent_idx|
cx.tcx.inherent_impls(DefId { index: parent_idx, krate: def_id.krate }).iter()
.find_map(|impl_id| cx.tcx.associated_items(*impl_id)
.find_by_name_and_kind(
cx.tcx,
Ident::from_str(method_name),
AssocKind::Fn,
*impl_id,
)
)
)
.map_or(def_id, |item| item.def_id)
} else {
def_id
};

// Check if the target item is a diagnostic item or LangItem.
let (msg, item) = if let Some(item_name)
= cx.tcx.diagnostic_items(def_id.krate).id_to_name.get(&def_id)
Expand All @@ -98,9 +120,7 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryDefPath {
"use of a def path to a diagnostic item",
Item::DiagnosticItem(*item_name),
)
} else if let Some(lang_item) = cx.tcx.lang_items().items().iter().position(|id| *id == Some(def_id)) {
let lang_items = def_path_res(cx, &["rustc_hir", "lang_items", "LangItem"], Some(Namespace::TypeNS)).def_id();
let item_name = cx.tcx.adt_def(lang_items).variants().iter().nth(lang_item).unwrap().name;
} else if let Some(item_name) = get_lang_item_name(cx, def_id) {
(
"use of a def path to a `LangItem`",
Item::LangItem(item_name),
Expand Down Expand Up @@ -168,10 +188,10 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryDefPath {
span_lint_and_then(
cx,
UNNECESSARY_DEF_PATH,
expr.span,
span,
msg,
|diag| {
diag.span_suggestion(expr.span, "try", sugg, app);
diag.span_suggestion(span, "try", sugg, app);
if with_note {
diag.help(
"if this `DefId` came from a constructor expression or pattern then the \
Expand All @@ -180,9 +200,19 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryDefPath {
}
},
);

self.linted_def_ids.insert(def_id);
}
}
}

fn check_array(&mut self, cx: &LateContext<'_>, elements: &[Expr<'_>], span: Span) {
let Some(path) = path_from_array(elements) else { return };

if let Some(def_id) = inherent_def_path_res(cx, &path.iter().map(AsRef::as_ref).collect::<Vec<_>>()) {
self.array_def_ids.insert((def_id, span));
}
}
}

fn path_to_matched_type(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option<Vec<String>> {
Expand All @@ -209,18 +239,7 @@ fn path_to_matched_type(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option<Ve
},
_ => None,
},
ExprKind::Array(exprs) => exprs
.iter()
.map(|expr| {
if let ExprKind::Lit(lit) = &expr.kind {
if let LitKind::Str(sym, _) = lit.node {
return Some((*sym.as_str()).to_owned());
}
}

None
})
.collect(),
ExprKind::Array(exprs) => path_from_array(exprs),
_ => None,
}
}
Expand Down Expand Up @@ -258,3 +277,67 @@ fn read_mir_alloc_def_path<'tcx>(cx: &LateContext<'tcx>, alloc: &'tcx Allocation
None
}
}

fn path_from_array(exprs: &[Expr<'_>]) -> Option<Vec<String>> {
exprs
.iter()
.map(|expr| {
if let ExprKind::Lit(lit) = &expr.kind {
if let LitKind::Str(sym, _) = lit.node {
return Some((*sym.as_str()).to_owned());
}
}

None
})
.collect()
}

// def_path_res will match field names before anything else, but for this we want to match
// inherent functions first.
fn inherent_def_path_res(cx: &LateContext<'_>, segments: &[&str]) -> Option<DefId> {
def_path_res(cx, segments, None).opt_def_id().map(|def_id| {
if cx.tcx.def_kind(def_id) == DefKind::Field {
let method_name = *segments.last().unwrap();
cx.tcx
.def_key(def_id)
.parent
.and_then(|parent_idx| {
cx.tcx
.inherent_impls(DefId {
index: parent_idx,
krate: def_id.krate,
})
.iter()
.find_map(|impl_id| {
cx.tcx.associated_items(*impl_id).find_by_name_and_kind(
cx.tcx,
Ident::from_str(method_name),
AssocKind::Fn,
*impl_id,
)
})
})
.map_or(def_id, |item| item.def_id)
} else {
def_id
}
})
}

fn get_lang_item_name(cx: &LateContext<'_>, def_id: DefId) -> Option<Symbol> {
if let Some(lang_item) = cx.tcx.lang_items().items().iter().position(|id| *id == Some(def_id)) {
let lang_items = def_path_res(cx, &["rustc_hir", "lang_items", "LangItem"], Some(Namespace::TypeNS)).def_id();
let item_name = cx
.tcx
.adt_def(lang_items)
.variants()
.iter()
.nth(lang_item)
.unwrap()
.name;
Some(item_name)
} else {
None
}
}
6 changes: 3 additions & 3 deletions tests/ui-internal/unnecessary_def_path.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ use rustc_hir::Expr;
use rustc_lint::LateContext;
use rustc_middle::ty::Ty;

#[allow(unused)]
#[allow(unused, clippy::unnecessary_def_path)]
static OPTION: [&str; 3] = ["core", "option", "Option"];
#[allow(unused)]
#[allow(unused, clippy::unnecessary_def_path)]
const RESULT: &[&str] = &["core", "result", "Result"];

fn _f<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, did: DefId, expr: &Expr<'_>) {
let _ = is_type_diagnostic_item(cx, ty, sym::Option);
let _ = is_type_diagnostic_item(cx, ty, sym::Result);
let _ = is_type_diagnostic_item(cx, ty, sym::Result);

#[allow(unused)]
#[allow(unused, clippy::unnecessary_def_path)]
let rc_path = &["alloc", "rc", "Rc"];
let _ = is_type_diagnostic_item(cx, ty, sym::Rc);

Expand Down
6 changes: 3 additions & 3 deletions tests/ui-internal/unnecessary_def_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ use rustc_hir::Expr;
use rustc_lint::LateContext;
use rustc_middle::ty::Ty;

#[allow(unused)]
#[allow(unused, clippy::unnecessary_def_path)]
static OPTION: [&str; 3] = ["core", "option", "Option"];
#[allow(unused)]
#[allow(unused, clippy::unnecessary_def_path)]
const RESULT: &[&str] = &["core", "result", "Result"];

fn _f<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, did: DefId, expr: &Expr<'_>) {
let _ = match_type(cx, ty, &OPTION);
let _ = match_type(cx, ty, RESULT);
let _ = match_type(cx, ty, &["core", "result", "Result"]);

#[allow(unused)]
#[allow(unused, clippy::unnecessary_def_path)]
let rc_path = &["alloc", "rc", "Rc"];
let _ = clippy_utils::ty::match_type(cx, ty, rc_path);

Expand Down
16 changes: 16 additions & 0 deletions tests/ui-internal/unnecessary_def_path_hardcoded_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![feature(rustc_private)]
#![allow(unused)]
#![warn(clippy::unnecessary_def_path)]

extern crate rustc_hir;

use rustc_hir::LangItem;

fn main() {
const DEREF_TRAIT: [&str; 4] = ["core", "ops", "deref", "Deref"];
const DEREF_MUT_TRAIT: [&str; 4] = ["core", "ops", "deref", "DerefMut"];
const DEREF_TRAIT_METHOD: [&str; 5] = ["core", "ops", "deref", "Deref", "deref"];

// Don't lint, not yet a diagnostic or language item
const DEREF_MUT_TRAIT_METHOD: [&str; 5] = ["core", "ops", "deref", "DerefMut", "deref_mut"];
}
27 changes: 27 additions & 0 deletions tests/ui-internal/unnecessary_def_path_hardcoded_path.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error: hardcoded path to a language item
--> $DIR/unnecessary_def_path_hardcoded_path.rs:11:40
|
LL | const DEREF_MUT_TRAIT: [&str; 4] = ["core", "ops", "deref", "DerefMut"];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: convert all references to use `LangItem::DerefMut`
= note: `-D clippy::unnecessary-def-path` implied by `-D warnings`

error: hardcoded path to a diagnostic item
--> $DIR/unnecessary_def_path_hardcoded_path.rs:12:43
|
LL | const DEREF_TRAIT_METHOD: [&str; 5] = ["core", "ops", "deref", "Deref", "deref"];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: convert all references to use `sym::deref_method`

error: hardcoded path to a diagnostic item
--> $DIR/unnecessary_def_path_hardcoded_path.rs:10:36
|
LL | const DEREF_TRAIT: [&str; 4] = ["core", "ops", "deref", "Deref"];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: convert all references to use `sym::Deref`

error: aborting due to 3 previous errors