Skip to content
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

Enable trimmed paths #7668

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 8 additions & 10 deletions clippy_lints/src/default.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_sugg};
use clippy_utils::diagnostics::{span_clippy_lint, span_lint_and_note};
use clippy_utils::source::snippet_with_macro_callsite;
use clippy_utils::{any_parent_is_automatically_derived, contains_name, in_macro, match_def_path, paths};
use if_chain::if_chain;
Expand Down Expand Up @@ -90,18 +90,16 @@ impl LateLintPass<'_> for Default {
let expr_ty = cx.typeck_results().expr_ty(expr);
if let ty::Adt(def, ..) = expr_ty.kind();
then {
// TODO: Work out a way to put "whatever the imported way of referencing
// this type in this file" rather than a fully-qualified type.
let replacement = format!("{}::default()", cx.tcx.def_path_str(def.did));
span_lint_and_sugg(
span_clippy_lint(
cx,
DEFAULT_TRAIT_ACCESS,
expr.span,
&format!("calling `{}` is more clear than this expression", replacement),
"try",
replacement,
Applicability::Unspecified, // First resolve the TODO above
);
|diag| {
let replacement = format!("{}::default()", cx.tcx.def_path_str(def.did));
diag.build(&format!("calling `{}` is more clear than this expression", replacement))
.span_suggestion(expr.span, "try", replacement, Applicability::MaybeIncorrect);
}
)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/eta_reduction.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
use clippy_utils::diagnostics::{span_clippy_lint, span_lint_and_sugg, span_lint_and_then};
use clippy_utils::higher::VecArgs;
use clippy_utils::source::snippet_opt;
use clippy_utils::usage::UsedAfterExprVisitor;
Expand Down Expand Up @@ -144,9 +144,9 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction {
let call_ty = cx.tcx.type_of(method_def_id).subst(cx.tcx, substs);
if check_sig(cx, closure_ty, call_ty);
then {
span_lint_and_then(cx, REDUNDANT_CLOSURE_FOR_METHOD_CALLS, expr.span, "redundant closure", |diag| {
span_clippy_lint(cx, REDUNDANT_CLOSURE_FOR_METHOD_CALLS, expr.span, |diag| {
let name = get_ufcs_type_name(cx, method_def_id);
diag.span_suggestion(
diag.build("redundant closure").span_suggestion(
expr.span,
"replace the closure with the method itself",
format!("{}::{}", name, path.ident.name),
Expand Down
11 changes: 6 additions & 5 deletions clippy_lints/src/from_over_into.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::diagnostics::span_clippy_lint;
use clippy_utils::{meets_msrv, msrvs};
use if_chain::if_chain;
use rustc_hir as hir;
Expand Down Expand Up @@ -64,13 +64,14 @@ impl LateLintPass<'_> for FromOverInto {
if cx.tcx.is_diagnostic_item(sym::into_trait, impl_trait_ref.def_id);

then {
span_lint_and_help(
span_clippy_lint(
cx,
FROM_OVER_INTO,
cx.tcx.sess.source_map().guess_head_span(item.span),
"an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true",
None,
&format!("consider to implement `From<{}>` instead", impl_trait_ref.self_ty()),
|diag| {
diag.build("an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true")
.help(&format!("consider to implement `From<{}>` instead", impl_trait_ref.self_ty()));
}
);
}
}
Expand Down
5 changes: 3 additions & 2 deletions clippy_lints/src/macro_use.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::in_macro;
use clippy_utils::source::snippet;
use hir::def::{DefKind, Res};
use if_chain::if_chain;
use rustc_ast::ast;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::{edition::Edition, sym, Span};

Expand Down Expand Up @@ -118,7 +119,7 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
for kid in cx.tcx.item_children(id).iter() {
if let Res::Def(DefKind::Macro(_mac_type), mac_id) = kid.res {
let span = mac_attr.span;
let def_path = cx.tcx.def_path_str(mac_id);
let def_path = with_no_trimmed_paths(|| cx.tcx.def_path_str(mac_id));
self.imports.push((def_path, span));
}
}
Expand Down
19 changes: 10 additions & 9 deletions clippy_lints/src/unnecessary_wraps.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::diagnostics::span_clippy_lint;
use clippy_utils::source::snippet;
use clippy_utils::{contains_return, in_macro, is_lang_ctor, return_ty, visitors::find_all_ret_expressions};
use if_chain::if_chain;
Expand Down Expand Up @@ -142,7 +142,10 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
}
});

if can_sugg && !suggs.is_empty() {
if !can_sugg || suggs.is_empty() {
return;
}
span_clippy_lint(cx, UNNECESSARY_WRAPS, span, |diag| {
let (lint_msg, return_type_sugg_msg, return_type_sugg, body_sugg_msg) = if inner_type.is_unit() {
(
"this function's return value is unnecessary".to_string(),
Expand All @@ -161,16 +164,14 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
"...and then change returning expressions",
)
};

span_lint_and_then(cx, UNNECESSARY_WRAPS, span, lint_msg.as_str(), |diag| {
diag.span_suggestion(
diag.build(&lint_msg)
.span_suggestion(
fn_decl.output.span(),
return_type_sugg_msg.as_str(),
return_type_sugg,
Applicability::MaybeIncorrect,
);
diag.multipart_suggestion(body_sugg_msg, suggs, Applicability::MaybeIncorrect);
});
}
)
.multipart_suggestion(body_sugg_msg, suggs, Applicability::MaybeIncorrect);
});
}
}
22 changes: 11 additions & 11 deletions clippy_lints/src/useless_conversion.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
use clippy_utils::diagnostics::{span_clippy_lint, span_lint_and_help, span_lint_and_sugg};
use clippy_utils::source::{snippet, snippet_with_macro_callsite};
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::{is_type_diagnostic_item, same_type_and_consts};
Expand Down Expand Up @@ -90,16 +90,16 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
let a = cx.typeck_results().expr_ty(e);
let b = cx.typeck_results().expr_ty(&args[0]);
if same_type_and_consts(a, b) {
let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
span_lint_and_sugg(
cx,
USELESS_CONVERSION,
e.span,
&format!("useless conversion to the same type: `{}`", b),
"consider removing `.into_iter()`",
sugg,
Applicability::MachineApplicable, // snippet
);
span_clippy_lint(cx, USELESS_CONVERSION, e.span, |diag| {
let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
diag.build(&format!("useless conversion to the same type: `{}`", b))
.span_suggestion(
e.span,
"consider removing `.into_iter()`",
sugg,
Applicability::MachineApplicable,
);
});
}
}
if_chain! {
Expand Down
32 changes: 23 additions & 9 deletions clippy_lints/src/utils/internal_lints.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use clippy_utils::consts::{constant_simple, Constant};
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
use clippy_utils::diagnostics::{
span_clippy_lint, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then,
};
use clippy_utils::higher;
use clippy_utils::source::snippet;
use clippy_utils::ty::match_type;
Expand All @@ -25,6 +27,7 @@ use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintCon
use rustc_middle::hir::map::Map;
use rustc_middle::mir::interpret::ConstValue;
use rustc_middle::ty;
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
use rustc_span::source_map::Spanned;
use rustc_span::symbol::{Symbol, SymbolStr};
Expand Down Expand Up @@ -941,15 +944,21 @@ impl<'tcx> LateLintPass<'tcx> for InterningDefinedSymbol {
let value = Symbol::intern(&arg).as_u32();
if let Some(&def_id) = self.symbol_map.get(&value);
then {
span_lint_and_sugg(
let span = is_expn_of(expr.span, "sym").unwrap_or(expr.span);
span_clippy_lint(
cx,
INTERNING_DEFINED_SYMBOL,
is_expn_of(expr.span, "sym").unwrap_or(expr.span),
"interning a defined symbol",
"try",
cx.tcx.def_path_str(def_id),
Applicability::MachineApplicable,
);
span,
|diag| {
diag.build("interning a defined symbol")
.span_suggestion(
span,
"try",
sym_sugg(cx, def_id),
Applicability::MachineApplicable,
);
}
)
}
}
if let ExprKind::Binary(op, left, right) = expr.kind {
Expand Down Expand Up @@ -1067,7 +1076,7 @@ impl<'tcx> SymbolStrExpr<'tcx> {
/// Returns a snippet that evaluates to a `Symbol` and is const if possible
fn as_symbol_snippet(&self, cx: &LateContext<'_>) -> Cow<'tcx, str> {
match *self {
Self::Const(def_id) => cx.tcx.def_path_str(def_id).into(),
Self::Const(def_id) => sym_sugg(cx, def_id).into(),
Self::Expr { item, is_ident, .. } => {
let mut snip = snippet(cx, item.span.source_callsite(), "..");
if is_ident {
Expand All @@ -1080,6 +1089,11 @@ impl<'tcx> SymbolStrExpr<'tcx> {
}
}

fn sym_sugg(cx: &LateContext<'_>, def_id: DefId) -> String {
// trimmed path does not include the parent module
with_no_trimmed_paths(|| cx.tcx.def_path_str(def_id))
}

declare_lint_pass!(IfChainStyle => [IF_CHAIN_STYLE]);

impl<'tcx> LateLintPass<'tcx> for IfChainStyle {
Expand Down
Loading