= method_names.iter().map(|s| s.as_str()).collect();
let method_names: Vec<&str> = method_names.iter().map(|s| &**s).collect();
@@ -1826,7 +1826,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
#[allow(clippy::too_many_lines)]
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
- if in_external_macro(cx.sess(), impl_item.span) {
+ if in_external_macro(cx.sess(), cx.tcx.hir().span_with_body(impl_item.hir_id())) {
return;
}
let name = impl_item.ident.name.as_str();
@@ -1862,10 +1862,11 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
fn_header_equals(method_config.fn_header, sig.header) &&
method_config.lifetime_param_cond(&impl_item)
{
+ let impl_item_span = cx.tcx.hir().span_with_body(impl_item.hir_id());
span_lint_and_help(
cx,
SHOULD_IMPLEMENT_TRAIT,
- impl_item.span,
+ impl_item_span,
&format!(
"method `{}` can be confused for the standard trait method `{}::{}`",
method_config.method_name,
@@ -1888,7 +1889,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
item.vis.node.is_pub(),
self_ty,
first_arg_ty,
- first_arg.pat.span
+ cx.tcx.hir().span(first_arg.pat.hir_id)
);
}
}
@@ -1918,7 +1919,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
span_lint(
cx,
NEW_RET_NO_SELF,
- impl_item.span,
+ cx.tcx.hir().span_with_body(impl_item.hir_id()),
"methods called `new` usually return `Self`",
);
}
@@ -1926,14 +1927,15 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
}
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'_>) {
- if in_external_macro(cx.tcx.sess, item.span) {
+ let item_span = cx.tcx.hir().span_with_body(item.hir_id());
+ if in_external_macro(cx.tcx.sess, item_span) {
return;
}
if_chain! {
if let TraitItemKind::Fn(ref sig, _) = item.kind;
if let Some(first_arg_ty) = sig.decl.inputs.iter().next();
- let first_arg_span = first_arg_ty.span;
+ let first_arg_span = cx.tcx.hir().span(first_arg_ty.hir_id);
let first_arg_ty = hir_ty_to_ty(cx.tcx, first_arg_ty);
let self_ty = TraitRef::identity(cx.tcx, item.def_id.to_def_id()).self_ty();
@@ -1960,7 +1962,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
span_lint(
cx,
NEW_RET_NO_SELF,
- item.span,
+ item_span,
"methods called `new` usually return `Self`",
);
}
@@ -2047,7 +2049,7 @@ fn lint_chars_cmp(
suggest: &str,
) -> bool {
if_chain! {
- if let Some(args) = method_chain_args(info.chain, chain_methods);
+ if let Some(args) = method_chain_args(cx, info.chain, chain_methods);
if let hir::ExprKind::Call(ref fun, ref arg_char) = info.other.kind;
if arg_char.len() == 1;
if let hir::ExprKind::Path(ref qpath) = fun.kind;
@@ -2064,14 +2066,14 @@ fn lint_chars_cmp(
span_lint_and_sugg(
cx,
lint,
- info.expr.span,
+ cx.tcx.hir().span(info.expr.hir_id),
&format!("you should use the `{}` method", suggest),
"like this",
format!("{}{}.{}({})",
if info.eq { "" } else { "!" },
- snippet_with_applicability(cx, args[0][0].span, "..", &mut applicability),
+ snippet_with_applicability(cx, cx.tcx.hir().span(args[0][0].hir_id), "..", &mut applicability),
suggest,
- snippet_with_applicability(cx, arg_char[0].span, "..", &mut applicability)),
+ snippet_with_applicability(cx, cx.tcx.hir().span(arg_char[0].hir_id), "..", &mut applicability)),
applicability,
);
@@ -2105,7 +2107,7 @@ fn lint_chars_cmp_with_unwrap<'tcx>(
suggest: &str,
) -> bool {
if_chain! {
- if let Some(args) = method_chain_args(info.chain, chain_methods);
+ if let Some(args) = method_chain_args(cx, info.chain, chain_methods);
if let hir::ExprKind::Lit(ref lit) = info.other.kind;
if let ast::LitKind::Char(c) = lit.node;
then {
@@ -2113,12 +2115,12 @@ fn lint_chars_cmp_with_unwrap<'tcx>(
span_lint_and_sugg(
cx,
lint,
- info.expr.span,
+ cx.tcx.hir().span(info.expr.hir_id),
&format!("you should use the `{}` method", suggest),
"like this",
format!("{}{}.{}('{}')",
if info.eq { "" } else { "!" },
- snippet_with_applicability(cx, args[0][0].span, "..", &mut applicability),
+ snippet_with_applicability(cx, cx.tcx.hir().span(args[0][0].hir_id), "..", &mut applicability),
suggest,
c),
applicability,
@@ -2156,7 +2158,7 @@ fn get_hint_if_single_char_arg(
let string = r.as_str();
if string.chars().count() == 1;
then {
- let snip = snippet_with_applicability(cx, arg.span, &string, applicability);
+ let snip = snippet_with_applicability(cx, cx.tcx.hir().span(arg.hir_id), "..", applicability);
let ch = if let ast::StrStyle::Raw(nhash) = style {
let nhash = nhash as usize;
// for raw string: r##"a"##
diff --git a/src/tools/clippy/clippy_lints/src/methods/ok_expect.rs b/src/tools/clippy/clippy_lints/src/methods/ok_expect.rs
index c1706cc7cc7d2..6342be8a845a8 100644
--- a/src/tools/clippy/clippy_lints/src/methods/ok_expect.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/ok_expect.rs
@@ -20,7 +20,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, ok_args: &[hir::
span_lint_and_help(
cx,
OK_EXPECT,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"called `ok().expect()` on a `Result` value",
None,
"you can call `expect()` directly on the `Result`",
diff --git a/src/tools/clippy/clippy_lints/src/methods/option_as_ref_deref.rs b/src/tools/clippy/clippy_lints/src/methods/option_as_ref_deref.rs
index 89067dbfe0e51..884fbd5c84137 100644
--- a/src/tools/clippy/clippy_lints/src/methods/option_as_ref_deref.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/option_as_ref_deref.rs
@@ -96,12 +96,22 @@ pub(super) fn check<'tcx>(
if is_deref {
let current_method = if is_mut {
- format!(".as_mut().map({})", snippet(cx, map_args[1].span, ".."))
+ format!(
+ ".as_mut().map({})",
+ snippet(cx, cx.tcx.hir().span(map_args[1].hir_id), "..")
+ )
} else {
- format!(".as_ref().map({})", snippet(cx, map_args[1].span, ".."))
+ format!(
+ ".as_ref().map({})",
+ snippet(cx, cx.tcx.hir().span(map_args[1].hir_id), "..")
+ )
};
let method_hint = if is_mut { "as_deref_mut" } else { "as_deref" };
- let hint = format!("{}.{}()", snippet(cx, as_ref_args[0].span, ".."), method_hint);
+ let hint = format!(
+ "{}.{}()",
+ snippet(cx, cx.tcx.hir().span(as_ref_args[0].hir_id), ".."),
+ method_hint
+ );
let suggestion = format!("try using {} instead", method_hint);
let msg = format!(
@@ -112,7 +122,7 @@ pub(super) fn check<'tcx>(
span_lint_and_sugg(
cx,
OPTION_AS_REF_DEREF,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
&msg,
&suggestion,
hint,
diff --git a/src/tools/clippy/clippy_lints/src/methods/option_map_or_none.rs b/src/tools/clippy/clippy_lints/src/methods/option_map_or_none.rs
index 64f6ebc5062ef..ab00e5ea16be0 100644
--- a/src/tools/clippy/clippy_lints/src/methods/option_map_or_none.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/option_map_or_none.rs
@@ -40,8 +40,8 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, map
};
if is_option {
- let self_snippet = snippet(cx, map_or_args[0].span, "..");
- let func_snippet = snippet(cx, map_or_args[2].span, "..");
+ let self_snippet = snippet(cx, cx.tcx.hir().span(map_or_args[0].hir_id), "..");
+ let func_snippet = snippet(cx, cx.tcx.hir().span(map_or_args[2].hir_id), "..");
let msg = "called `map_or(None, ..)` on an `Option` value. This can be done more directly by calling \
`and_then(..)` instead";
(
@@ -53,7 +53,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, map
} else if f_arg_is_some {
let msg = "called `map_or(None, Some)` on a `Result` value. This can be done more directly by calling \
`ok()` instead";
- let self_snippet = snippet(cx, map_or_args[0].span, "..");
+ let self_snippet = snippet(cx, cx.tcx.hir().span(map_or_args[0].hir_id), "..");
(
RESULT_MAP_OR_INTO_OPTION,
msg,
@@ -69,7 +69,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, map
span_lint_and_sugg(
cx,
lint_name,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
msg,
instead,
hint,
diff --git a/src/tools/clippy/clippy_lints/src/methods/option_map_unwrap_or.rs b/src/tools/clippy/clippy_lints/src/methods/option_map_unwrap_or.rs
index 7cdd49bbf0307..91d138cb82882 100644
--- a/src/tools/clippy/clippy_lints/src/methods/option_map_unwrap_or.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/option_map_unwrap_or.rs
@@ -43,13 +43,13 @@ pub(super) fn check<'tcx>(
}
}
- if differing_macro_contexts(unwrap_args[1].span, map_span) {
+ if differing_macro_contexts(cx.tcx.hir().span(unwrap_args[1].hir_id), map_span) {
return;
}
let mut applicability = Applicability::MachineApplicable;
// get snippet for unwrap_or()
- let unwrap_snippet = snippet_with_applicability(cx, unwrap_args[1].span, "..", &mut applicability);
+ let unwrap_snippet = snippet_with_applicability(cx, cx.tcx.hir().span(unwrap_args[1].hir_id), "..", &mut applicability);
// lint message
// comparing the snippet from source to raw text ("None") below is safe
// because we already have checked the type.
@@ -66,15 +66,15 @@ pub(super) fn check<'tcx>(
arg, suggest
);
- span_lint_and_then(cx, MAP_UNWRAP_OR, expr.span, msg, |diag| {
- let map_arg_span = map_args[1].span;
+ span_lint_and_then(cx, MAP_UNWRAP_OR, cx.tcx.hir().span(expr.hir_id), msg, |diag| {
+ let map_arg_span = cx.tcx.hir().span(map_args[1].hir_id);
let mut suggestion = vec![
(
map_span,
String::from(if unwrap_snippet_none { "and_then" } else { "map_or" }),
),
- (expr.span.with_lo(unwrap_args[0].span.hi()), String::from("")),
+ (cx.tcx.hir().span(expr.hir_id).with_lo(cx.tcx.hir().span(unwrap_args[0].hir_id).hi()), String::from("")),
];
if !unwrap_snippet_none {
diff --git a/src/tools/clippy/clippy_lints/src/methods/or_fun_call.rs b/src/tools/clippy/clippy_lints/src/methods/or_fun_call.rs
index 5f7fc431d2248..b6dc12fac0193 100644
--- a/src/tools/clippy/clippy_lints/src/methods/or_fun_call.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/or_fun_call.rs
@@ -53,7 +53,7 @@ pub(super) fn check<'tcx>(
"try this",
format!(
"{}.unwrap_or_default()",
- snippet_with_applicability(cx, self_expr.span, "..", &mut applicability)
+ snippet_with_applicability(cx, cx.tcx.hir().span(self_expr.hir_id), "..", &mut applicability)
),
applicability,
);
@@ -118,7 +118,7 @@ pub(super) fn check<'tcx>(
let sugg: Cow<'_, str> = {
let (snippet_span, use_lambda) = match (fn_has_arguments, fun_span) {
(false, Some(fun_span)) => (fun_span, false),
- _ => (arg.span, true),
+ _ => (cx.tcx.hir().span(arg.hir_id), true),
};
let snippet = {
let not_macro_argument_snippet = snippet_with_macro_callsite(cx, snippet_span, "..");
@@ -159,13 +159,41 @@ pub(super) fn check<'tcx>(
match args[1].kind {
hir::ExprKind::Call(ref fun, ref or_args) => {
let or_has_args = !or_args.is_empty();
- if !check_unwrap_or_default(cx, name, fun, &args[0], &args[1], or_has_args, expr.span) {
- let fun_span = if or_has_args { None } else { Some(fun.span) };
- check_general_case(cx, name, method_span, &args[0], &args[1], expr.span, fun_span);
+ if !check_unwrap_or_default(
+ cx,
+ name,
+ fun,
+ &args[0],
+ &args[1],
+ or_has_args,
+ cx.tcx.hir().span(expr.hir_id),
+ ) {
+ let fun_span = if or_has_args {
+ None
+ } else {
+ Some(cx.tcx.hir().span(fun.hir_id))
+ };
+ check_general_case(
+ cx,
+ name,
+ method_span,
+ &args[0],
+ &args[1],
+ cx.tcx.hir().span(expr.hir_id),
+ fun_span,
+ );
}
},
hir::ExprKind::Index(..) | hir::ExprKind::MethodCall(..) => {
- check_general_case(cx, name, method_span, &args[0], &args[1], expr.span, None);
+ check_general_case(
+ cx,
+ name,
+ method_span,
+ &args[0],
+ &args[1],
+ cx.tcx.hir().span(expr.hir_id),
+ None,
+ );
},
_ => {},
}
diff --git a/src/tools/clippy/clippy_lints/src/methods/search_is_some.rs b/src/tools/clippy/clippy_lints/src/methods/search_is_some.rs
index e9e654432208d..92a9764428d29 100644
--- a/src/tools/clippy/clippy_lints/src/methods/search_is_some.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/search_is_some.rs
@@ -30,7 +30,7 @@ pub(super) fn check<'tcx>(
search_method
);
let hint = "this is more succinctly expressed by calling `any()`";
- let search_snippet = snippet(cx, search_args[1].span, "..");
+ let search_snippet = snippet(cx, cx.tcx.hir().span(search_args[1].hir_id), "..");
if search_snippet.lines().count() <= 1 {
// suggest `any(|x| ..)` instead of `any(|&x| ..)` for `find(|&x| ..).is_some()`
// suggest `any(|..| *..)` instead of `any(|..| **..)` for `find(|..| **..).is_some()`
@@ -56,7 +56,7 @@ pub(super) fn check<'tcx>(
span_lint_and_sugg(
cx,
SEARCH_IS_SOME,
- method_span.with_hi(expr.span.hi()),
+ method_span.with_hi(cx.tcx.hir().span(expr.hir_id).hi()),
&msg,
"use `any()` instead",
format!(
@@ -66,7 +66,7 @@ pub(super) fn check<'tcx>(
Applicability::MachineApplicable,
);
} else {
- span_lint_and_help(cx, SEARCH_IS_SOME, expr.span, &msg, None, hint);
+ span_lint_and_help(cx, SEARCH_IS_SOME, cx.tcx.hir().span(expr.hir_id), &msg, None, hint);
}
}
// lint if `find()` is called by `String` or `&str`
@@ -85,11 +85,11 @@ pub(super) fn check<'tcx>(
then {
let msg = "called `is_some()` after calling `find()` on a string";
let mut applicability = Applicability::MachineApplicable;
- let find_arg = snippet_with_applicability(cx, search_args[1].span, "..", &mut applicability);
+ let find_arg = snippet_with_applicability(cx, cx.tcx.hir().span(search_args[1].hir_id), "..", &mut applicability);
span_lint_and_sugg(
cx,
SEARCH_IS_SOME,
- method_span.with_hi(expr.span.hi()),
+ method_span.with_hi(cx.tcx.hir().span(expr.hir_id).hi()),
msg,
"use `contains()` instead",
format!("contains({})", find_arg),
diff --git a/src/tools/clippy/clippy_lints/src/methods/single_char_insert_string.rs b/src/tools/clippy/clippy_lints/src/methods/single_char_insert_string.rs
index 0ce8b66978dc1..2982785e9e77f 100644
--- a/src/tools/clippy/clippy_lints/src/methods/single_char_insert_string.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/single_char_insert_string.rs
@@ -10,14 +10,18 @@ use super::SINGLE_CHAR_ADD_STR;
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
let mut applicability = Applicability::MachineApplicable;
if let Some(extension_string) = get_hint_if_single_char_arg(cx, &args[2], &mut applicability) {
- let base_string_snippet =
- snippet_with_applicability(cx, args[0].span.source_callsite(), "_", &mut applicability);
- let pos_arg = snippet_with_applicability(cx, args[1].span, "..", &mut applicability);
+ let base_string_snippet = snippet_with_applicability(
+ cx,
+ cx.tcx.hir().span(args[0].hir_id).source_callsite(),
+ "_",
+ &mut applicability,
+ );
+ let pos_arg = snippet_with_applicability(cx, cx.tcx.hir().span(args[1].hir_id), "..", &mut applicability);
let sugg = format!("{}.insert({}, {})", base_string_snippet, pos_arg, extension_string);
span_lint_and_sugg(
cx,
SINGLE_CHAR_ADD_STR,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"calling `insert_str()` using a single-character string literal",
"consider using `insert` with a character literal",
sugg,
diff --git a/src/tools/clippy/clippy_lints/src/methods/single_char_pattern.rs b/src/tools/clippy/clippy_lints/src/methods/single_char_pattern.rs
index 61cbc9d2f0a62..82984c8e4bed5 100644
--- a/src/tools/clippy/clippy_lints/src/methods/single_char_pattern.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/single_char_pattern.rs
@@ -13,7 +13,7 @@ pub(super) fn check(cx: &LateContext<'_>, _expr: &hir::Expr<'_>, arg: &hir::Expr
span_lint_and_sugg(
cx,
SINGLE_CHAR_PATTERN,
- arg.span,
+ cx.tcx.hir().span(arg.hir_id),
"single-character string constant used as pattern",
"try using a `char` instead",
hint,
diff --git a/src/tools/clippy/clippy_lints/src/methods/single_char_push_string.rs b/src/tools/clippy/clippy_lints/src/methods/single_char_push_string.rs
index deacc70b713e5..3f2cd6c5975a2 100644
--- a/src/tools/clippy/clippy_lints/src/methods/single_char_push_string.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/single_char_push_string.rs
@@ -10,13 +10,17 @@ use super::SINGLE_CHAR_ADD_STR;
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
let mut applicability = Applicability::MachineApplicable;
if let Some(extension_string) = get_hint_if_single_char_arg(cx, &args[1], &mut applicability) {
- let base_string_snippet =
- snippet_with_applicability(cx, args[0].span.source_callsite(), "..", &mut applicability);
+ let base_string_snippet = snippet_with_applicability(
+ cx,
+ cx.tcx.hir().span(args[0].hir_id).source_callsite(),
+ "..",
+ &mut applicability,
+ );
let sugg = format!("{}.push({})", base_string_snippet, extension_string);
span_lint_and_sugg(
cx,
SINGLE_CHAR_ADD_STR,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"calling `push_str()` using a single-character string literal",
"consider using `push` with a character literal",
sugg,
diff --git a/src/tools/clippy/clippy_lints/src/methods/skip_while_next.rs b/src/tools/clippy/clippy_lints/src/methods/skip_while_next.rs
index 8ba6ae952003e..48cd6a53a9395 100644
--- a/src/tools/clippy/clippy_lints/src/methods/skip_while_next.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/skip_while_next.rs
@@ -11,7 +11,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, _sk
span_lint_and_help(
cx,
SKIP_WHILE_NEXT,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"called `skip_while().next()` on an `Iterator`",
None,
"this is more succinctly expressed by calling `.find(!
)` instead",
diff --git a/src/tools/clippy/clippy_lints/src/methods/string_extend_chars.rs b/src/tools/clippy/clippy_lints/src/methods/string_extend_chars.rs
index 0a08ea26175fe..e42addac23bf9 100644
--- a/src/tools/clippy/clippy_lints/src/methods/string_extend_chars.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/string_extend_chars.rs
@@ -11,7 +11,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Exp
let obj_ty = cx.typeck_results().expr_ty(&args[0]).peel_refs();
if is_type_diagnostic_item(cx, obj_ty, sym::string_type) {
let arg = &args[1];
- if let Some(arglists) = method_chain_args(arg, &["chars"]) {
+ if let Some(arglists) = method_chain_args(cx, arg, &["chars"]) {
let target = &arglists[0][0];
let self_ty = cx.typeck_results().expr_ty(target).peel_refs();
let ref_str = if *self_ty.kind() == ty::Str {
@@ -26,14 +26,14 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Exp
span_lint_and_sugg(
cx,
STRING_EXTEND_CHARS,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"calling `.extend(_.chars())`",
"try this",
format!(
"{}.push_str({}{})",
- snippet_with_applicability(cx, args[0].span, "..", &mut applicability),
+ snippet_with_applicability(cx, cx.tcx.hir().span(args[0].hir_id), "..", &mut applicability),
ref_str,
- snippet_with_applicability(cx, target.span, "..", &mut applicability)
+ snippet_with_applicability(cx, cx.tcx.hir().span(target.hir_id), "..", &mut applicability)
),
applicability,
);
diff --git a/src/tools/clippy/clippy_lints/src/methods/suspicious_map.rs b/src/tools/clippy/clippy_lints/src/methods/suspicious_map.rs
index e135a826dc4d0..a00bd9fe7e3b1 100644
--- a/src/tools/clippy/clippy_lints/src/methods/suspicious_map.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/suspicious_map.rs
@@ -8,7 +8,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
span_lint_and_help(
cx,
SUSPICIOUS_MAP,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"this call to `map()` won't have an effect on the call to `count()`",
None,
"make sure you did not confuse `map` with `filter` or `for_each`",
diff --git a/src/tools/clippy/clippy_lints/src/methods/uninit_assumed_init.rs b/src/tools/clippy/clippy_lints/src/methods/uninit_assumed_init.rs
index 798b66192c818..10ba8c38928c0 100644
--- a/src/tools/clippy/clippy_lints/src/methods/uninit_assumed_init.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/uninit_assumed_init.rs
@@ -18,7 +18,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, outer: &hir::Exp
span_lint(
cx,
UNINIT_ASSUMED_INIT,
- outer.span,
+ cx.tcx.hir().span(outer.hir_id),
"this call for this type may be undefined behavior"
);
}
diff --git a/src/tools/clippy/clippy_lints/src/methods/unnecessary_filter_map.rs b/src/tools/clippy/clippy_lints/src/methods/unnecessary_filter_map.rs
index 12b2cf0a16582..b4347c64ab8c0 100644
--- a/src/tools/clippy/clippy_lints/src/methods/unnecessary_filter_map.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/unnecessary_filter_map.rs
@@ -31,7 +31,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Exp
span_lint(
cx,
UNNECESSARY_FILTER_MAP,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"this `.filter_map` can be written more simply using `.map`",
);
return;
@@ -41,7 +41,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Exp
span_lint(
cx,
UNNECESSARY_FILTER_MAP,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"this `.filter_map` can be written more simply using `.filter`",
);
return;
diff --git a/src/tools/clippy/clippy_lints/src/methods/unnecessary_fold.rs b/src/tools/clippy/clippy_lints/src/methods/unnecessary_fold.rs
index a26443f4ee944..1948cfdbf2386 100644
--- a/src/tools/clippy/clippy_lints/src/methods/unnecessary_fold.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/unnecessary_fold.rs
@@ -47,7 +47,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, fold_args: &[hir
"{replacement}(|{s}| {r})",
replacement = replacement_method_name,
s = second_arg_ident,
- r = snippet_with_applicability(cx, right_expr.span, "EXPR", &mut applicability),
+ r = snippet_with_applicability(cx, cx.tcx.hir().span(right_expr.hir_id), "EXPR", &mut applicability),
)
} else {
format!(
@@ -59,7 +59,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, fold_args: &[hir
span_lint_and_sugg(
cx,
UNNECESSARY_FOLD,
- fold_span.with_hi(expr.span.hi()),
+ fold_span.with_hi(cx.tcx.hir().span(expr.hir_id).hi()),
// TODO #2371 don't suggest e.g., .any(|x| f(x)) if we can suggest .any(f)
"this `.fold` can be written more succinctly using another method",
"try",
diff --git a/src/tools/clippy/clippy_lints/src/methods/unnecessary_lazy_eval.rs b/src/tools/clippy/clippy_lints/src/methods/unnecessary_lazy_eval.rs
index a17259d697faa..d3206c0c1ed1c 100644
--- a/src/tools/clippy/clippy_lints/src/methods/unnecessary_lazy_eval.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/unnecessary_lazy_eval.rs
@@ -48,14 +48,14 @@ pub(super) fn check<'tcx>(
span_lint_and_sugg(
cx,
UNNECESSARY_LAZY_EVALUATIONS,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
msg,
&format!("use `{}` instead", simplify_using),
format!(
"{0}.{1}({2})",
- snippet(cx, args[0].span, ".."),
+ snippet(cx, cx.tcx.hir().span(args[0].hir_id), ".."),
simplify_using,
- snippet(cx, body_expr.span, ".."),
+ snippet(cx, cx.tcx.hir().span(body_expr.hir_id), ".."),
),
applicability,
);
diff --git a/src/tools/clippy/clippy_lints/src/methods/unwrap_used.rs b/src/tools/clippy/clippy_lints/src/methods/unwrap_used.rs
index 094c3fc45c493..438320c6bf5ff 100644
--- a/src/tools/clippy/clippy_lints/src/methods/unwrap_used.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/unwrap_used.rs
@@ -21,7 +21,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, unwrap_args: &[h
span_lint_and_help(
cx,
lint,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
&format!("used `unwrap()` on `{}` value", kind,),
None,
&format!(
diff --git a/src/tools/clippy/clippy_lints/src/methods/useless_asref.rs b/src/tools/clippy/clippy_lints/src/methods/useless_asref.rs
index e4554f8d4897e..cba45b9120a49 100644
--- a/src/tools/clippy/clippy_lints/src/methods/useless_asref.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/useless_asref.rs
@@ -24,7 +24,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str,
if_chain! {
if let Some(parent) = get_parent_expr(cx, expr);
if let hir::ExprKind::MethodCall(_, ref span, _, _) = parent.kind;
- if span != &expr.span;
+ if span != &cx.tcx.hir().span(expr.hir_id);
then {
return;
}
@@ -34,10 +34,10 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str,
span_lint_and_sugg(
cx,
USELESS_ASREF,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
&format!("this call to `{}` does nothing", call_name),
"try this",
- snippet_with_applicability(cx, recvr.span, "..", &mut applicability).to_string(),
+ snippet_with_applicability(cx, cx.tcx.hir().span(recvr.hir_id), "..", &mut applicability).to_string(),
applicability,
);
}
diff --git a/src/tools/clippy/clippy_lints/src/methods/zst_offset.rs b/src/tools/clippy/clippy_lints/src/methods/zst_offset.rs
index f1335726736ca..5a609e97650b8 100644
--- a/src/tools/clippy/clippy_lints/src/methods/zst_offset.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/zst_offset.rs
@@ -13,7 +13,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Exp
if let Ok(layout) = cx.tcx.layout_of(cx.param_env.and(ty));
if layout.is_zst();
then {
- span_lint(cx, ZST_OFFSET, expr.span, "offset calculation on zero-sized value");
+ span_lint(cx, ZST_OFFSET, cx.tcx.hir().span(expr.hir_id), "offset calculation on zero-sized value");
}
}
}
diff --git a/src/tools/clippy/clippy_lints/src/minmax.rs b/src/tools/clippy/clippy_lints/src/minmax.rs
index 8d0c3b8e0fe89..6fd4b89a3968e 100644
--- a/src/tools/clippy/clippy_lints/src/minmax.rs
+++ b/src/tools/clippy/clippy_lints/src/minmax.rs
@@ -48,7 +48,7 @@ impl<'tcx> LateLintPass<'tcx> for MinMaxPass {
span_lint(
cx,
MIN_MAX,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"this `min`/`max` combination leads to constant result",
);
},
diff --git a/src/tools/clippy/clippy_lints/src/misc.rs b/src/tools/clippy/clippy_lints/src/misc.rs
index acdc245456b5a..c44fabe3bf243 100644
--- a/src/tools/clippy/clippy_lints/src/misc.rs
+++ b/src/tools/clippy/clippy_lints/src/misc.rs
@@ -276,13 +276,13 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
k: FnKind<'tcx>,
decl: &'tcx FnDecl<'_>,
body: &'tcx Body<'_>,
- span: Span,
- _: HirId,
+ hir_id: HirId,
) {
if let FnKind::Closure = k {
// Does not apply to closures
return;
}
+ let span = cx.tcx.hir().span(hir_id);
if in_external_macro(cx.tcx.sess, span) {
return;
}
@@ -291,7 +291,7 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
span_lint(
cx,
TOPLEVEL_REF_ARG,
- arg.pat.span,
+ cx.tcx.hir().span(arg.pat.hir_id),
"`ref` directly on a function argument is ignored. \
Consider using a reference type instead",
);
@@ -301,7 +301,8 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
if_chain! {
- if !in_external_macro(cx.tcx.sess, stmt.span);
+ let stmt_span = cx.tcx.hir().span(stmt.hir_id);
+ if !in_external_macro(cx.tcx.sess, stmt_span);
if let StmtKind::Local(ref local) = stmt.kind;
if let PatKind::Binding(an, .., name, None) = local.pat.kind;
if let Some(ref init) = local.init;
@@ -310,7 +311,8 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
if an == BindingAnnotation::Ref || an == BindingAnnotation::RefMut {
// use the macro callsite when the init span (but not the whole local span)
// comes from an expansion like `vec![1, 2, 3]` in `let ref _ = vec![1, 2, 3];`
- let sugg_init = if init.span.from_expansion() && !local.span.from_expansion() {
+ let local_span = cx.tcx.hir().span(local.hir_id);
+ let sugg_init = if cx.tcx.hir().span(init.hir_id).from_expansion() && !local_span.from_expansion() {
Sugg::hir_with_macro_callsite(cx, init, "..")
} else {
Sugg::hir(cx, init, "..")
@@ -321,7 +323,8 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
("", sugg_init.addr())
};
let tyopt = if let Some(ref ty) = local.ty {
- format!(": &{mutopt}{ty}", mutopt=mutopt, ty=snippet(cx, ty.span, ".."))
+ let ty_span = cx.tcx.hir().span(ty.hir_id);
+ format!(": &{mutopt}{ty}", mutopt=mutopt, ty=snippet(cx, ty_span, ".."))
} else {
String::new()
};
@@ -329,11 +332,11 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
cx,
TOPLEVEL_REF_ARG,
init.hir_id,
- local.pat.span,
+ cx.tcx.hir().span(local.pat.hir_id),
"`ref` on an entire `let` pattern is discouraged, take a reference with `&` instead",
|diag| {
diag.span_suggestion(
- stmt.span,
+ cx.tcx.hir().span(stmt.hir_id),
"try",
format!(
"let {name}{tyopt} = {initref};",
@@ -354,19 +357,20 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
if binop.node == BinOpKind::And || binop.node == BinOpKind::Or;
if let Some(sugg) = Sugg::hir_opt(cx, a);
then {
+ let stmt_span = cx.tcx.hir().span(stmt.hir_id);
span_lint_and_then(cx,
SHORT_CIRCUIT_STATEMENT,
- stmt.span,
+ stmt_span,
"boolean short circuit operator in statement may be clearer using an explicit test",
|diag| {
let sugg = if binop.node == BinOpKind::Or { !sugg } else { sugg };
diag.span_suggestion(
- stmt.span,
+ stmt_span,
"replace it with",
format!(
"if {} {{ {}; }}",
sugg,
- &snippet(cx, b.span, ".."),
+ &snippet(cx, cx.tcx.hir().span(b.hir_id), ".."),
),
Applicability::MachineApplicable, // snippet
);
@@ -376,9 +380,10 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
}
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
+ let expr_span = cx.tcx.hir().span(expr.hir_id);
match expr.kind {
ExprKind::Cast(ref e, ref ty) => {
- check_cast(cx, expr.span, e, ty);
+ check_cast(cx, expr_span, e, ty);
return;
},
ExprKind::Binary(ref cmp, ref left, ref right) => {
@@ -387,7 +392,7 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
},
_ => {},
}
- if in_attributes_expansion(expr) || expr.span.is_desugaring(DesugaringKind::Await) {
+ if in_attributes_expansion(cx, expr) || expr_span.is_desugaring(DesugaringKind::Await) {
// Don't lint things expanded by #[derive(...)], etc or `await` desugaring
return;
}
@@ -420,7 +425,7 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
span_lint(
cx,
USED_UNDERSCORE_BINDING,
- expr.span,
+ expr_span,
&format!(
"used binding `{}` which is prefixed with an underscore. A leading \
underscore signals that a binding will not be used",
@@ -471,7 +476,7 @@ fn check_nan(cx: &LateContext<'_>, expr: &Expr<'_>, cmp_expr: &Expr<'_>) {
span_lint(
cx,
CMP_NAN,
- cmp_expr.span,
+ cx.tcx.hir().span(cmp_expr.hir_id),
"doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead",
);
}
@@ -560,7 +565,7 @@ fn check_to_owned(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left:
if is_diagnostic_assoc_item(cx, expr_def_id, sym::ToString)
|| is_diagnostic_assoc_item(cx, expr_def_id, sym::ToOwned);
then {
- (cx.typeck_results().expr_ty(&args[0]), snippet(cx, args[0].span, ".."))
+ (cx.typeck_results().expr_ty(&args[0]), snippet(cx, cx.tcx.hir().span(args[0].hir_id), ".."))
} else {
return;
}
@@ -569,7 +574,10 @@ fn check_to_owned(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left:
ExprKind::Call(ref path, ref v) if v.len() == 1 => {
if let ExprKind::Path(ref path) = path.kind {
if match_qpath(path, &["String", "from_str"]) || match_qpath(path, &["String", "from"]) {
- (cx.typeck_results().expr_ty(&v[0]), snippet(cx, v[0].span, ".."))
+ (
+ cx.typeck_results().expr_ty(&v[0]),
+ snippet(cx, cx.tcx.hir().span(v[0].hir_id), ".."),
+ )
} else {
return;
}
@@ -595,9 +603,9 @@ fn check_to_owned(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left:
let other_gets_derefed = matches!(other.kind, ExprKind::Unary(UnOp::Deref, _));
let lint_span = if other_gets_derefed {
- expr.span.to(other.span)
+ cx.tcx.hir().span(expr.hir_id).to(cx.tcx.hir().span(other.hir_id))
} else {
- expr.span
+ cx.tcx.hir().span(expr.hir_id)
};
span_lint_and_then(
@@ -622,17 +630,20 @@ fn check_to_owned(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left:
eq_impl = without_deref;
};
+ let expr_span = cx.tcx.hir().span(expr.hir_id);
+ let other_span = cx.tcx.hir().span(other.hir_id);
+
let span;
let hint;
if (eq_impl.ty_eq_other && left) || (eq_impl.other_eq_ty && !left) {
- span = expr.span;
+ span = expr_span;
hint = expr_snip;
} else {
- span = expr.span.to(other.span);
+ span = expr_span.to(other_span);
if eq_impl.ty_eq_other {
- hint = format!("{} == {}", expr_snip, snippet(cx, other.span, ".."));
+ hint = format!("{} == {}", expr_snip, snippet(cx, other_span, ".."));
} else {
- hint = format!("{} == {}", snippet(cx, other.span, ".."), expr_snip);
+ hint = format!("{} == {}", snippet(cx, other_span, ".."), expr_snip);
}
}
@@ -658,11 +669,17 @@ fn is_used(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
/// Tests whether an expression is in a macro expansion (e.g., something
/// generated by `#[derive(...)]` or the like).
-fn in_attributes_expansion(expr: &Expr<'_>) -> bool {
+fn in_attributes_expansion(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
use rustc_span::hygiene::MacroKind;
- if expr.span.from_expansion() {
- let data = expr.span.ctxt().outer_expn_data();
- matches!(data.kind, ExpnKind::Macro(MacroKind::Attr, _))
+ let expr_span = cx.tcx.hir().span(expr.hir_id);
+ if expr_span.from_expansion() {
+ let data = expr_span.ctxt().outer_expn_data();
+
+ if let ExpnKind::Macro(MacroKind::Attr, _) = data.kind {
+ true
+ } else {
+ false
+ }
} else {
false
}
@@ -691,7 +708,7 @@ fn check_cast(cx: &LateContext<'_>, span: Span, e: &Expr<'_>, ty: &hir::Ty<'_>)
let (sugg, appl) = if let TyKind::Infer = mut_ty.ty.kind {
(format!("{}()", sugg_fn), Applicability::MachineApplicable)
- } else if let Some(mut_ty_snip) = snippet_opt(cx, mut_ty.ty.span) {
+ } else if let Some(mut_ty_snip) = snippet_opt(cx, cx.tcx.hir().span(mut_ty.ty.hir_id)) {
(format!("{}::<{}>()", sugg_fn, mut_ty_snip), Applicability::MachineApplicable)
} else {
// `MaybeIncorrect` as type inference may not work with the suggested code
@@ -737,13 +754,14 @@ fn check_binary(
is_named_constant(cx, left) || is_named_constant(cx, right),
is_comparing_arrays,
);
- span_lint_and_then(cx, lint, expr.span, msg, |diag| {
+ let expr_span = cx.tcx.hir().span(expr.hir_id);
+ span_lint_and_then(cx, lint, expr_span, msg, |diag| {
let lhs = Sugg::hir(cx, left, "..");
let rhs = Sugg::hir(cx, right, "..");
if !is_comparing_arrays {
diag.span_suggestion(
- expr.span,
+ expr_span,
"consider comparing them within some margin of error",
format!(
"({}).abs() {} error_margin",
@@ -756,8 +774,9 @@ fn check_binary(
diag.note("`f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`");
});
} else if op == BinOpKind::Rem {
+ let expr_span = cx.tcx.hir().span(expr.hir_id);
if is_integer_const(cx, right, 1) {
- span_lint(cx, MODULO_ONE, expr.span, "any number modulo 1 will be 0");
+ span_lint(cx, MODULO_ONE, expr_span, "any number modulo 1 will be 0");
}
if let ty::Int(ity) = cx.typeck_results().expr_ty(right).kind() {
@@ -765,7 +784,7 @@ fn check_binary(
span_lint(
cx,
MODULO_ONE,
- expr.span,
+ expr_span,
"any number modulo -1 will panic/overflow or result in 0",
);
}
diff --git a/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs b/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs
index b0998a80128ce..8dc114d84fb5f 100644
--- a/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs
+++ b/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs
@@ -9,7 +9,6 @@ use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_semver::RustcVersion;
use rustc_session::{declare_tool_lint, impl_lint_pass};
-use rustc_span::Span;
use rustc_typeck::hir_ty_to_ty;
const MISSING_CONST_FOR_FN_MSRV: RustcVersion = RustcVersion::new(1, 37, 0);
@@ -94,7 +93,6 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
kind: FnKind<'_>,
_: &FnDecl<'_>,
_: &Body<'_>,
- span: Span,
hir_id: HirId,
) {
if !meets_msrv(self.msrv.as_ref(), &MISSING_CONST_FOR_FN_MSRV) {
@@ -102,6 +100,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
}
let def_id = cx.tcx.hir().local_def_id(hir_id);
+ let span = cx.tcx.hir().span_with_body(hir_id);
if in_external_macro(cx.tcx.sess, span) || is_entrypoint_fn(cx, def_id.to_def_id()) {
return;
diff --git a/src/tools/clippy/clippy_lints/src/missing_doc.rs b/src/tools/clippy/clippy_lints/src/missing_doc.rs
index 6ec4c38d0f9cc..107b633098f1f 100644
--- a/src/tools/clippy/clippy_lints/src/missing_doc.rs
+++ b/src/tools/clippy/clippy_lints/src/missing_doc.rs
@@ -126,9 +126,10 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
}
- fn check_crate(&mut self, cx: &LateContext<'tcx>, krate: &'tcx hir::Crate<'_>) {
+ fn check_crate(&mut self, cx: &LateContext<'tcx>, _: &'tcx hir::Crate<'_>) {
let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID);
- self.check_missing_docs_attrs(cx, attrs, krate.item.span, "the", "crate");
+ let span = cx.tcx.hir().span(hir::CRATE_HIR_ID);
+ self.check_missing_docs_attrs(cx, attrs, span, "the", "crate");
}
fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
@@ -162,14 +163,20 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
let (article, desc) = cx.tcx.article_and_description(it.def_id.to_def_id());
let attrs = cx.tcx.hir().attrs(it.hir_id());
- self.check_missing_docs_attrs(cx, attrs, it.span, article, desc);
+ self.check_missing_docs_attrs(cx, attrs, cx.tcx.hir().span_with_body(it.hir_id()), article, desc);
}
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, trait_item: &'tcx hir::TraitItem<'_>) {
let (article, desc) = cx.tcx.article_and_description(trait_item.def_id.to_def_id());
let attrs = cx.tcx.hir().attrs(trait_item.hir_id());
- self.check_missing_docs_attrs(cx, attrs, trait_item.span, article, desc);
+ self.check_missing_docs_attrs(
+ cx,
+ attrs,
+ cx.tcx.hir().span_with_body(trait_item.hir_id()),
+ article,
+ desc,
+ );
}
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
@@ -185,18 +192,26 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
let (article, desc) = cx.tcx.article_and_description(impl_item.def_id.to_def_id());
let attrs = cx.tcx.hir().attrs(impl_item.hir_id());
- self.check_missing_docs_attrs(cx, attrs, impl_item.span, article, desc);
+ self.check_missing_docs_attrs(
+ cx,
+ attrs,
+ cx.tcx.hir().span_with_body(impl_item.hir_id()),
+ article,
+ desc,
+ );
}
fn check_struct_field(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::StructField<'_>) {
if !sf.is_positional() {
let attrs = cx.tcx.hir().attrs(sf.hir_id);
- self.check_missing_docs_attrs(cx, attrs, sf.span, "a", "struct field");
+ let span = cx.tcx.hir().span(sf.hir_id);
+ self.check_missing_docs_attrs(cx, attrs, span, "a", "struct field");
}
}
fn check_variant(&mut self, cx: &LateContext<'tcx>, v: &'tcx hir::Variant<'_>) {
let attrs = cx.tcx.hir().attrs(v.id);
- self.check_missing_docs_attrs(cx, attrs, v.span, "a", "variant");
+ let span = cx.tcx.hir().span(v.id);
+ self.check_missing_docs_attrs(cx, attrs, span, "a", "variant");
}
}
diff --git a/src/tools/clippy/clippy_lints/src/missing_inline.rs b/src/tools/clippy/clippy_lints/src/missing_inline.rs
index da59c820999d9..a049c7f1736c6 100644
--- a/src/tools/clippy/clippy_lints/src/missing_inline.rs
+++ b/src/tools/clippy/clippy_lints/src/missing_inline.rs
@@ -83,7 +83,9 @@ declare_lint_pass!(MissingInline => [MISSING_INLINE_IN_PUBLIC_ITEMS]);
impl<'tcx> LateLintPass<'tcx> for MissingInline {
fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
- if rustc_middle::lint::in_external_macro(cx.sess(), it.span) || is_executable_or_proc_macro(cx) {
+ if rustc_middle::lint::in_external_macro(cx.sess(), cx.tcx.hir().span_with_body(it.hir_id()))
+ || is_executable_or_proc_macro(cx)
+ {
return;
}
@@ -94,7 +96,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {
hir::ItemKind::Fn(..) => {
let desc = "a function";
let attrs = cx.tcx.hir().attrs(it.hir_id());
- check_missing_inline_attrs(cx, attrs, it.span, desc);
+ check_missing_inline_attrs(cx, attrs, cx.tcx.hir().span_with_body(it.hir_id()), desc);
},
hir::ItemKind::Trait(ref _is_auto, ref _unsafe, ref _generics, ref _bounds, trait_items) => {
// note: we need to check if the trait is exported so we can't use
@@ -110,7 +112,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {
let desc = "a default trait method";
let item = cx.tcx.hir().trait_item(tit.id);
let attrs = cx.tcx.hir().attrs(item.hir_id());
- check_missing_inline_attrs(cx, attrs, item.span, desc);
+ check_missing_inline_attrs(cx, attrs, cx.tcx.hir().span_with_body(item.hir_id()), desc);
}
},
}
@@ -135,7 +137,9 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
use rustc_middle::ty::{ImplContainer, TraitContainer};
- if rustc_middle::lint::in_external_macro(cx.sess(), impl_item.span) || is_executable_or_proc_macro(cx) {
+ if rustc_middle::lint::in_external_macro(cx.sess(), cx.tcx.hir().span_with_body(impl_item.hir_id()))
+ || is_executable_or_proc_macro(cx)
+ {
return;
}
@@ -163,6 +167,6 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {
}
let attrs = cx.tcx.hir().attrs(impl_item.hir_id());
- check_missing_inline_attrs(cx, attrs, impl_item.span, desc);
+ check_missing_inline_attrs(cx, attrs, cx.tcx.hir().span_with_body(impl_item.hir_id()), desc);
}
}
diff --git a/src/tools/clippy/clippy_lints/src/modulo_arithmetic.rs b/src/tools/clippy/clippy_lints/src/modulo_arithmetic.rs
index da3ae1d652f6c..8cf0567dd34fc 100644
--- a/src/tools/clippy/clippy_lints/src/modulo_arithmetic.rs
+++ b/src/tools/clippy/clippy_lints/src/modulo_arithmetic.rs
@@ -89,7 +89,7 @@ fn check_const_operands<'tcx>(
span_lint_and_then(
cx,
MODULO_ARITHMETIC,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
&format!(
"you are using modulo operator on constants with different signs: `{} % {}`",
lhs_operand.string_representation.as_ref().unwrap(),
@@ -111,7 +111,7 @@ fn check_non_const_operands<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>,
span_lint_and_then(
cx,
MODULO_ARITHMETIC,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"you are using modulo operator on types that might have different signs",
|diag| {
diag.note("double check for expected result especially when interoperating with different languages");
diff --git a/src/tools/clippy/clippy_lints/src/mut_key.rs b/src/tools/clippy/clippy_lints/src/mut_key.rs
index 908b7bb7ce00d..ce6d00a8564ef 100644
--- a/src/tools/clippy/clippy_lints/src/mut_key.rs
+++ b/src/tools/clippy/clippy_lints/src/mut_key.rs
@@ -79,7 +79,8 @@ impl<'tcx> LateLintPass<'tcx> for MutableKeyType {
if let hir::PatKind::Wild = local.pat.kind {
return;
}
- check_ty(cx, local.span, cx.typeck_results().pat_ty(&*local.pat));
+ let local_span = cx.tcx.hir().span(local.hir_id);
+ check_ty(cx, local_span, cx.typeck_results().pat_ty(&*local.pat));
}
}
@@ -87,9 +88,13 @@ fn check_sig<'tcx>(cx: &LateContext<'tcx>, item_hir_id: hir::HirId, decl: &hir::
let fn_def_id = cx.tcx.hir().local_def_id(item_hir_id);
let fn_sig = cx.tcx.fn_sig(fn_def_id);
for (hir_ty, ty) in decl.inputs.iter().zip(fn_sig.inputs().skip_binder().iter()) {
- check_ty(cx, hir_ty.span, ty);
+ check_ty(cx, cx.tcx.hir().span(hir_ty.hir_id), ty);
}
- check_ty(cx, decl.output.span(), cx.tcx.erase_late_bound_regions(fn_sig.output()));
+ check_ty(
+ cx,
+ decl.output.span(|id| cx.tcx.hir().span(id)),
+ cx.tcx.erase_late_bound_regions(fn_sig.output()),
+ );
}
// We want to lint 1. sets or maps with 2. not immutable key types and 3. no unerased
diff --git a/src/tools/clippy/clippy_lints/src/mut_mut.rs b/src/tools/clippy/clippy_lints/src/mut_mut.rs
index d7239b328bbcd..c66e838945480 100644
--- a/src/tools/clippy/clippy_lints/src/mut_mut.rs
+++ b/src/tools/clippy/clippy_lints/src/mut_mut.rs
@@ -48,11 +48,11 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for MutVisitor<'a, 'tcx> {
type Map = Map<'tcx>;
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
- if in_external_macro(self.cx.sess(), expr.span) {
+ if in_external_macro(self.cx.sess(), self.cx.tcx.hir().span(expr.hir_id)) {
return;
}
- if let Some((_, arg, body, _)) = higher::for_loop(expr) {
+ if let Some((_, arg, body, _)) = higher::for_loop(self.cx, expr) {
// A `for` loop lowers to:
// ```rust
// match ::std::iter::Iterator::next(&mut iter) {
@@ -66,14 +66,14 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for MutVisitor<'a, 'tcx> {
span_lint(
self.cx,
MUT_MUT,
- expr.span,
+ self.cx.tcx.hir().span(expr.hir_id),
"generally you want to avoid `&mut &mut _` if possible",
);
} else if let ty::Ref(_, _, hir::Mutability::Mut) = self.cx.typeck_results().expr_ty(e).kind() {
span_lint(
self.cx,
MUT_MUT,
- expr.span,
+ self.cx.tcx.hir().span(expr.hir_id),
"this expression mutably borrows a mutable reference. Consider reborrowing",
);
}
@@ -100,7 +100,7 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for MutVisitor<'a, 'tcx> {
span_lint(
self.cx,
MUT_MUT,
- ty.span,
+ self.cx.tcx.hir().span(ty.hir_id),
"generally you want to avoid `&mut &mut _` if possible",
);
}
diff --git a/src/tools/clippy/clippy_lints/src/mut_reference.rs b/src/tools/clippy/clippy_lints/src/mut_reference.rs
index 3f0b765df1561..7ef9d7eaa17c2 100644
--- a/src/tools/clippy/clippy_lints/src/mut_reference.rs
+++ b/src/tools/clippy/clippy_lints/src/mut_reference.rs
@@ -74,7 +74,7 @@ fn check_arguments<'tcx>(
span_lint(
cx,
UNNECESSARY_MUT_PASSED,
- argument.span,
+ cx.tcx.hir().span(argument.hir_id),
&format!("the {} `{}` doesn't need a mutable reference", fn_kind, name),
);
}
diff --git a/src/tools/clippy/clippy_lints/src/mutable_debug_assertion.rs b/src/tools/clippy/clippy_lints/src/mutable_debug_assertion.rs
index 9caacb5db7c9c..08b207529e863 100644
--- a/src/tools/clippy/clippy_lints/src/mutable_debug_assertion.rs
+++ b/src/tools/clippy/clippy_lints/src/mutable_debug_assertion.rs
@@ -37,7 +37,7 @@ const DEBUG_MACRO_NAMES: [&str; 3] = ["debug_assert", "debug_assert_eq", "debug_
impl<'tcx> LateLintPass<'tcx> for DebugAssertWithMutCall {
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
for dmn in &DEBUG_MACRO_NAMES {
- if is_direct_expn_of(e.span, dmn).is_some() {
+ if is_direct_expn_of(cx.tcx.hir().span(e.hir_id), dmn).is_some() {
if let Some(macro_args) = higher::extract_assert_macro_args(e) {
for arg in macro_args {
let mut visitor = MutArgVisitor::new(cx);
@@ -103,7 +103,7 @@ impl<'a, 'tcx> Visitor<'tcx> for MutArgVisitor<'a, 'tcx> {
},
// Don't check await desugars
ExprKind::Match(_, _, MatchSource::AwaitDesugar) => return,
- _ if !self.found => self.expr_span = Some(expr.span),
+ _ if !self.found => self.expr_span = Some(self.cx.tcx.hir().span(expr.hir_id)),
_ => return,
}
walk_expr(self, expr)
diff --git a/src/tools/clippy/clippy_lints/src/mutex_atomic.rs b/src/tools/clippy/clippy_lints/src/mutex_atomic.rs
index 40b236493a313..2faf180cd8a1a 100644
--- a/src/tools/clippy/clippy_lints/src/mutex_atomic.rs
+++ b/src/tools/clippy/clippy_lints/src/mutex_atomic.rs
@@ -75,10 +75,11 @@ impl<'tcx> LateLintPass<'tcx> for Mutex {
behavior and not the internal type, consider using `Mutex<()>`",
atomic_name
);
+ let expr_span = cx.tcx.hir().span(expr.hir_id);
match *mutex_param.kind() {
- ty::Uint(t) if t != ty::UintTy::Usize => span_lint(cx, MUTEX_INTEGER, expr.span, &msg),
- ty::Int(t) if t != ty::IntTy::Isize => span_lint(cx, MUTEX_INTEGER, expr.span, &msg),
- _ => span_lint(cx, MUTEX_ATOMIC, expr.span, &msg),
+ ty::Uint(t) if t != ty::UintTy::Usize => span_lint(cx, MUTEX_INTEGER, expr_span, &msg),
+ ty::Int(t) if t != ty::IntTy::Isize => span_lint(cx, MUTEX_INTEGER, expr_span, &msg),
+ _ => span_lint(cx, MUTEX_ATOMIC, expr_span, &msg),
};
}
}
diff --git a/src/tools/clippy/clippy_lints/src/needless_bool.rs b/src/tools/clippy/clippy_lints/src/needless_bool.rs
index f283ff1715fb6..c73dc88f492ed 100644
--- a/src/tools/clippy/clippy_lints/src/needless_bool.rs
+++ b/src/tools/clippy/clippy_lints/src/needless_bool.rs
@@ -86,7 +86,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBool {
span_lint_and_sugg(
cx,
NEEDLESS_BOOL,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
"this if-then-else expression returns a bool literal",
"you can reduce it to",
snip.to_string(),
@@ -99,7 +99,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBool {
span_lint(
cx,
NEEDLESS_BOOL,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
"this if-then-else expression will always return true",
);
},
@@ -107,7 +107,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBool {
span_lint(
cx,
NEEDLESS_BOOL,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
"this if-then-else expression will always return false",
);
},
@@ -128,7 +128,7 @@ declare_lint_pass!(BoolComparison => [BOOL_COMPARISON]);
impl<'tcx> LateLintPass<'tcx> for BoolComparison {
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
- if e.span.from_expansion() {
+ if cx.tcx.hir().span(e.hir_id).from_expansion() {
return;
}
@@ -194,16 +194,20 @@ struct ExpressionInfoWithSpan {
right_span: Span,
}
-fn is_unary_not(e: &Expr<'_>) -> (bool, Span) {
+fn is_unary_not(cx: &LateContext<'_>, e: &Expr<'_>) -> (bool, Span) {
if let ExprKind::Unary(UnOp::Not, operand) = e.kind {
- return (true, operand.span);
+ return (true, cx.tcx.hir().span(operand.hir_id));
}
- (false, e.span)
+ (false, cx.tcx.hir().span(e.hir_id))
}
-fn one_side_is_unary_not<'tcx>(left_side: &'tcx Expr<'_>, right_side: &'tcx Expr<'_>) -> ExpressionInfoWithSpan {
- let left = is_unary_not(left_side);
- let right = is_unary_not(right_side);
+fn one_side_is_unary_not<'tcx>(
+ cx: &LateContext<'tcx>,
+ left_side: &'tcx Expr<'_>,
+ right_side: &'tcx Expr<'_>,
+) -> ExpressionInfoWithSpan {
+ let left = is_unary_not(cx, left_side);
+ let right = is_unary_not(cx, right_side);
ExpressionInfoWithSpan {
one_side_is_unary_not: left.0 != right.0,
@@ -228,19 +232,21 @@ fn check_comparison<'a, 'tcx>(
cx.typeck_results().expr_ty(left_side),
cx.typeck_results().expr_ty(right_side),
);
- if is_expn_of(left_side.span, "cfg").is_some() || is_expn_of(right_side.span, "cfg").is_some() {
+ if is_expn_of(cx.tcx.hir().span(left_side.hir_id), "cfg").is_some()
+ || is_expn_of(cx.tcx.hir().span(right_side.hir_id), "cfg").is_some()
+ {
return;
}
if l_ty.is_bool() && r_ty.is_bool() {
let mut applicability = Applicability::MachineApplicable;
if let BinOpKind::Eq = op.node {
- let expression_info = one_side_is_unary_not(&left_side, &right_side);
+ let expression_info = one_side_is_unary_not(cx, &left_side, &right_side);
if expression_info.one_side_is_unary_not {
span_lint_and_sugg(
cx,
BOOL_COMPARISON,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
"this comparison might be written more concisely",
"try simplifying it as shown",
format!(
@@ -272,7 +278,7 @@ fn check_comparison<'a, 'tcx>(
span_lint_and_sugg(
cx,
BOOL_COMPARISON,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
m,
"try simplifying it as shown",
h(left_side, right_side).to_string(),
@@ -293,7 +299,7 @@ fn suggest_bool_comparison<'a, 'tcx>(
message: &str,
conv_hint: impl FnOnce(Sugg<'a>) -> Sugg<'a>,
) {
- let hint = if expr.span.from_expansion() {
+ let hint = if cx.tcx.hir().span(expr.hir_id).from_expansion() {
if applicability != Applicability::Unspecified {
applicability = Applicability::MaybeIncorrect;
}
@@ -304,7 +310,7 @@ fn suggest_bool_comparison<'a, 'tcx>(
span_lint_and_sugg(
cx,
BOOL_COMPARISON,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
message,
"try simplifying it as shown",
conv_hint(hint).to_string(),
diff --git a/src/tools/clippy/clippy_lints/src/needless_borrow.rs b/src/tools/clippy/clippy_lints/src/needless_borrow.rs
index 1aadcfd87b60f..74083756d9546 100644
--- a/src/tools/clippy/clippy_lints/src/needless_borrow.rs
+++ b/src/tools/clippy/clippy_lints/src/needless_borrow.rs
@@ -43,7 +43,8 @@ impl_lint_pass!(NeedlessBorrow => [NEEDLESS_BORROW]);
impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
- if e.span.from_expansion() || self.derived_item.is_some() {
+ let e_span = cx.tcx.hir().span(e.hir_id);
+ if e_span.from_expansion() || self.derived_item.is_some() {
return;
}
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, ref inner) = e.kind {
@@ -61,16 +62,16 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
span_lint_and_then(
cx,
NEEDLESS_BORROW,
- e.span,
+ e_span,
&format!(
"this expression borrows a reference (`&{}`) that is immediately dereferenced \
by the compiler",
ty
),
|diag| {
- if let Some(snippet) = snippet_opt(cx, inner.span) {
+ if let Some(snippet) = snippet_opt(cx, cx.tcx.hir().span(inner.hir_id)) {
diag.span_suggestion(
- e.span,
+ e_span,
"change this to",
snippet,
Applicability::MachineApplicable,
@@ -84,7 +85,8 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
}
}
fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
- if pat.span.from_expansion() || self.derived_item.is_some() {
+ let pat_span = cx.tcx.hir().span(pat.hir_id);
+ if pat_span.from_expansion() || self.derived_item.is_some() {
return;
}
if_chain! {
@@ -98,12 +100,12 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
span_lint_and_then(
cx,
NEEDLESS_BORROW,
- pat.span,
+ pat_span,
"this pattern creates a reference to a reference",
|diag| {
if let Some(snippet) = snippet_opt(cx, name.span) {
diag.span_suggestion(
- pat.span,
+ pat_span,
"change this to",
snippet,
Applicability::MachineApplicable,
diff --git a/src/tools/clippy/clippy_lints/src/needless_borrowed_ref.rs b/src/tools/clippy/clippy_lints/src/needless_borrowed_ref.rs
index f449f397e7d61..20ddca6202e79 100644
--- a/src/tools/clippy/clippy_lints/src/needless_borrowed_ref.rs
+++ b/src/tools/clippy/clippy_lints/src/needless_borrowed_ref.rs
@@ -43,7 +43,8 @@ declare_lint_pass!(NeedlessBorrowedRef => [NEEDLESS_BORROWED_REFERENCE]);
impl<'tcx> LateLintPass<'tcx> for NeedlessBorrowedRef {
fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
- if pat.span.from_expansion() {
+ let pat_span = cx.tcx.hir().span(pat.hir_id);
+ if pat_span.from_expansion() {
// OK, simple enough, lints doesn't check in macro.
return;
}
@@ -64,12 +65,12 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrowedRef {
return;
}
let mut applicability = Applicability::MachineApplicable;
- span_lint_and_then(cx, NEEDLESS_BORROWED_REFERENCE, pat.span,
+ span_lint_and_then(cx, NEEDLESS_BORROWED_REFERENCE, pat_span,
"this pattern takes a reference on something that is being de-referenced",
|diag| {
let hint = snippet_with_applicability(cx, spanned_name.span, "..", &mut applicability).into_owned();
diag.span_suggestion(
- pat.span,
+ pat_span,
"try removing the `&ref` part and just keep",
hint,
applicability,
diff --git a/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs b/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs
index cac4b2075114a..312df981c2962 100644
--- a/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs
+++ b/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs
@@ -72,9 +72,9 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
kind: FnKind<'tcx>,
decl: &'tcx FnDecl<'_>,
body: &'tcx Body<'_>,
- span: Span,
hir_id: HirId,
) {
+ let span = cx.tcx.hir().span(hir_id);
if span.from_expansion() {
return;
}
@@ -144,7 +144,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
for (idx, ((input, &ty), arg)) in decl.inputs.iter().zip(fn_sig.inputs()).zip(body.params).enumerate() {
// All spans generated from a proc-macro invocation are the same...
- if span == input.span {
+ if span == cx.tcx.hir().span(input.hir_id) {
return;
}
@@ -215,9 +215,9 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
_ => None,
}).unwrap());
then {
- let slice_ty = format!("&[{}]", snippet(cx, elem_ty.span, "_"));
+ let slice_ty = format!("&[{}]", snippet(cx, cx.tcx.hir().span(elem_ty.hir_id), "_"));
diag.span_suggestion(
- input.span,
+ cx.tcx.hir().span(input.hir_id),
"consider changing the type to",
slice_ty,
Applicability::Unspecified,
@@ -246,7 +246,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
if let Some(clone_spans) =
get_spans(cx, Some(body.id()), idx, &[("clone", ".to_string()"), ("as_str", "")]) {
diag.span_suggestion(
- input.span,
+ cx.tcx.hir().span(input.hir_id),
"consider changing the type to",
"&str".to_string(),
Applicability::Unspecified,
@@ -270,7 +270,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
}
}
- let mut spans = vec![(input.span, format!("&{}", snippet(cx, input.span, "_")))];
+ let mut spans = vec![(cx.tcx.hir().span(input.hir_id), format!("&{}", snippet(cx, cx.tcx.hir().span(input.hir_id), "_")))];
// Suggests adding `*` to dereference the added reference.
if let Some(deref_span) = deref_span {
@@ -288,7 +288,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
span_lint_and_then(
cx,
NEEDLESS_PASS_BY_VALUE,
- input.span,
+ cx.tcx.hir().span(input.hir_id),
"this argument is passed by value, but not consumed in the function body",
sugg,
);
diff --git a/src/tools/clippy/clippy_lints/src/needless_question_mark.rs b/src/tools/clippy/clippy_lints/src/needless_question_mark.rs
index a3293f1b36149..9fdf834b93321 100644
--- a/src/tools/clippy/clippy_lints/src/needless_question_mark.rs
+++ b/src/tools/clippy/clippy_lints/src/needless_question_mark.rs
@@ -141,10 +141,13 @@ fn emit_lint(cx: &LateContext<'_>, expr: &SomeOkCall<'_>) {
utils::span_lint_and_sugg(
cx,
NEEDLESS_QUESTION_MARK,
- entire_expr.span,
+ cx.tcx.hir().span(entire_expr.hir_id),
"question mark operator is useless here",
"try",
- format!("{}", utils::snippet(cx, inner_expr.span, r#""...""#)),
+ format!(
+ "{}",
+ utils::snippet(cx, cx.tcx.hir().span(inner_expr.hir_id), r#""...""#)
+ ),
Applicability::MachineApplicable,
);
}
diff --git a/src/tools/clippy/clippy_lints/src/needless_update.rs b/src/tools/clippy/clippy_lints/src/needless_update.rs
index 41cf541ecf5ef..70d4a5cf1c221 100644
--- a/src/tools/clippy/clippy_lints/src/needless_update.rs
+++ b/src/tools/clippy/clippy_lints/src/needless_update.rs
@@ -58,7 +58,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessUpdate {
span_lint(
cx,
NEEDLESS_UPDATE,
- base.span,
+ cx.tcx.hir().span(base.hir_id),
"struct update has no effect, all the fields in the struct have already been specified",
);
}
diff --git a/src/tools/clippy/clippy_lints/src/neg_cmp_op_on_partial_ord.rs b/src/tools/clippy/clippy_lints/src/neg_cmp_op_on_partial_ord.rs
index ec0ad58ca9c3e..221801d417ea9 100644
--- a/src/tools/clippy/clippy_lints/src/neg_cmp_op_on_partial_ord.rs
+++ b/src/tools/clippy/clippy_lints/src/neg_cmp_op_on_partial_ord.rs
@@ -49,7 +49,7 @@ impl<'tcx> LateLintPass<'tcx> for NoNegCompOpForPartialOrd {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if_chain! {
- if !in_external_macro(cx.sess(), expr.span);
+ if !in_external_macro(cx.sess(), cx.tcx.hir().span(expr.hir_id));
if let ExprKind::Unary(UnOp::Not, ref inner) = expr.kind;
if let ExprKind::Binary(ref op, ref left, _) = inner.kind;
if let BinOpKind::Le | BinOpKind::Ge | BinOpKind::Lt | BinOpKind::Gt = op.node;
@@ -78,7 +78,7 @@ impl<'tcx> LateLintPass<'tcx> for NoNegCompOpForPartialOrd {
span_lint(
cx,
NEG_CMP_OP_ON_PARTIAL_ORD,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"the use of negated comparison operators on partially ordered \
types produces code that is hard to read and refactor, please \
consider using the `partial_cmp` method instead, to make it \
diff --git a/src/tools/clippy/clippy_lints/src/neg_multiply.rs b/src/tools/clippy/clippy_lints/src/neg_multiply.rs
index ef7cc65cfcf0a..0f79dbe13ace7 100644
--- a/src/tools/clippy/clippy_lints/src/neg_multiply.rs
+++ b/src/tools/clippy/clippy_lints/src/neg_multiply.rs
@@ -32,8 +32,8 @@ impl<'tcx> LateLintPass<'tcx> for NegMultiply {
if BinOpKind::Mul == op.node {
match (&left.kind, &right.kind) {
(&ExprKind::Unary(..), &ExprKind::Unary(..)) => {},
- (&ExprKind::Unary(UnOp::Neg, ref lit), _) => check_mul(cx, e.span, lit, right),
- (_, &ExprKind::Unary(UnOp::Neg, ref lit)) => check_mul(cx, e.span, lit, left),
+ (&ExprKind::Unary(UnOp::Neg, ref lit), _) => check_mul(cx, cx.tcx.hir().span(e.hir_id), lit, right),
+ (_, &ExprKind::Unary(UnOp::Neg, ref lit)) => check_mul(cx, cx.tcx.hir().span(e.hir_id), lit, left),
_ => {},
}
}
diff --git a/src/tools/clippy/clippy_lints/src/new_without_default.rs b/src/tools/clippy/clippy_lints/src/new_without_default.rs
index de2899c3462a4..c004f29eaf37a 100644
--- a/src/tools/clippy/clippy_lints/src/new_without_default.rs
+++ b/src/tools/clippy/clippy_lints/src/new_without_default.rs
@@ -67,7 +67,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
for assoc_item in items {
if let hir::AssocItemKind::Fn { has_self: false } = assoc_item.kind {
let impl_item = cx.tcx.hir().impl_item(assoc_item.id);
- if in_external_macro(cx.sess(), impl_item.span) {
+ if in_external_macro(cx.sess(), cx.tcx.hir().span_with_body(impl_item.hir_id())) {
return;
}
if let hir::ImplItemKind::Fn(ref sig, _) = impl_item.kind {
@@ -129,7 +129,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
cx,
NEW_WITHOUT_DEFAULT,
id,
- impl_item.span,
+ cx.tcx.hir().span_with_body(impl_item.hir_id()),
&format!(
"you should consider adding a `Default` implementation for `{}`",
self_ty
@@ -137,7 +137,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
|diag| {
diag.suggest_prepend_item(
cx,
- item.span,
+ cx.tcx.hir().span_with_body(item.hir_id()),
"try this",
&create_new_without_default_suggest_msg(self_ty),
Applicability::MaybeIncorrect,
diff --git a/src/tools/clippy/clippy_lints/src/no_effect.rs b/src/tools/clippy/clippy_lints/src/no_effect.rs
index 69302d695ce0a..d958bf16bf0d1 100644
--- a/src/tools/clippy/clippy_lints/src/no_effect.rs
+++ b/src/tools/clippy/clippy_lints/src/no_effect.rs
@@ -43,7 +43,7 @@ declare_clippy_lint! {
}
fn has_no_effect(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
- if expr.span.from_expansion() {
+ if cx.tcx.hir().span(expr.hir_id).from_expansion() {
return false;
}
match expr.kind {
@@ -92,14 +92,14 @@ impl<'tcx> LateLintPass<'tcx> for NoEffect {
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
if let StmtKind::Semi(ref expr) = stmt.kind {
if has_no_effect(cx, expr) {
- span_lint(cx, NO_EFFECT, stmt.span, "statement with no effect");
+ span_lint(cx, NO_EFFECT, cx.tcx.hir().span(stmt.hir_id), "statement with no effect");
} else if let Some(reduced) = reduce_expression(cx, expr) {
let mut snippet = String::new();
for e in reduced {
- if e.span.from_expansion() {
+ if cx.tcx.hir().span(e.hir_id).from_expansion() {
return;
}
- if let Some(snip) = snippet_opt(cx, e.span) {
+ if let Some(snip) = snippet_opt(cx, cx.tcx.hir().span(e.hir_id)) {
snippet.push_str(&snip);
snippet.push(';');
} else {
@@ -109,7 +109,7 @@ impl<'tcx> LateLintPass<'tcx> for NoEffect {
span_lint_and_sugg(
cx,
UNNECESSARY_OPERATION,
- stmt.span,
+ cx.tcx.hir().span(stmt.hir_id),
"statement can be reduced",
"replace it with",
snippet,
@@ -121,7 +121,7 @@ impl<'tcx> LateLintPass<'tcx> for NoEffect {
}
fn reduce_expression<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option>> {
- if expr.span.from_expansion() {
+ if cx.tcx.hir().span(expr.hir_id).from_expansion() {
return None;
}
match expr.kind {
diff --git a/src/tools/clippy/clippy_lints/src/non_copy_const.rs b/src/tools/clippy/clippy_lints/src/non_copy_const.rs
index 8aebce67917af..454a0e44e0ee8 100644
--- a/src/tools/clippy/clippy_lints/src/non_copy_const.rs
+++ b/src/tools/clippy/clippy_lints/src/non_copy_const.rs
@@ -237,7 +237,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
if is_unfrozen(cx, ty) && is_value_unfrozen_poly(cx, body_id, ty) {
- lint(cx, Source::Item { item: it.span });
+ lint(cx, Source::Item { item: cx.tcx.hir().span(it.hir_id()) });
}
}
}
@@ -264,7 +264,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
// re-implementing the trait predicate evaluation specific to `Freeze`.
&& body_id_opt.map_or(true, |body_id| is_value_unfrozen_poly(cx, body_id, normalized))
{
- lint(cx, Source::Assoc { item: trait_item.span });
+ lint(cx, Source::Assoc { item: cx.tcx.hir().span(trait_item.hir_id()) });
}
}
}
@@ -310,7 +310,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
lint(
cx,
Source::Assoc {
- item: impl_item.span,
+ item: cx.tcx.hir().span(impl_item.hir_id()),
},
);
}
@@ -323,7 +323,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
let normalized = cx.tcx.normalize_erasing_regions(cx.param_env, ty);
if is_unfrozen(cx, ty) && is_value_unfrozen_poly(cx, *body_id, normalized) {
- lint(cx, Source::Assoc { item: impl_item.span });
+ lint(cx, Source::Assoc { item: cx.tcx.hir().span(impl_item.hir_id()) });
}
},
_ => (),
@@ -418,7 +418,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
};
if is_unfrozen(cx, ty) && is_value_unfrozen_expr(cx, expr.hir_id, item_def_id, ty) {
- lint(cx, Source::Expr { expr: expr.span });
+ lint(cx, Source::Expr { expr: cx.tcx.hir().span(expr.hir_id) });
}
}
}
diff --git a/src/tools/clippy/clippy_lints/src/open_options.rs b/src/tools/clippy/clippy_lints/src/open_options.rs
index 07ca196990da9..c90800c2eb736 100644
--- a/src/tools/clippy/clippy_lints/src/open_options.rs
+++ b/src/tools/clippy/clippy_lints/src/open_options.rs
@@ -34,7 +34,7 @@ impl<'tcx> LateLintPass<'tcx> for OpenOptions {
if path.ident.name == sym!(open) && match_type(cx, obj_ty, &paths::OPEN_OPTIONS) {
let mut options = Vec::new();
get_open_options(cx, &arguments[0], &mut options);
- check_open_options(cx, &options, e.span);
+ check_open_options(cx, &options, cx.tcx.hir().span(e.hir_id));
}
}
}
diff --git a/src/tools/clippy/clippy_lints/src/option_if_let_else.rs b/src/tools/clippy/clippy_lints/src/option_if_let_else.rs
index 9ef0d267b0b20..a3a6eac99f036 100644
--- a/src/tools/clippy/clippy_lints/src/option_if_let_else.rs
+++ b/src/tools/clippy/clippy_lints/src/option_if_let_else.rs
@@ -158,15 +158,15 @@ fn detect_option_if_let_else<'tcx>(
expr: &'_ Expr<'tcx>,
) -> Option {
if_chain! {
- if !utils::in_macro(expr.span); // Don't lint macros, because it behaves weirdly
+ if !utils::in_macro(cx.tcx.hir().span(expr.hir_id)); // Don't lint macros, because it behaves weirdly
if let ExprKind::Match(cond_expr, arms, MatchSource::IfLetDesugar{contains_else_clause: true}) = &expr.kind;
if arms.len() == 2;
if !is_result_ok(cx, cond_expr); // Don't lint on Result::ok because a different lint does it already
if let PatKind::TupleStruct(struct_qpath, &[inner_pat], _) = &arms[0].pat.kind;
if utils::match_qpath(struct_qpath, &paths::OPTION_SOME);
if let PatKind::Binding(bind_annotation, _, id, _) = &inner_pat.kind;
- if !utils::usage::contains_return_break_continue_macro(arms[0].body);
- if !utils::usage::contains_return_break_continue_macro(arms[1].body);
+ if !utils::usage::contains_return_break_continue_macro(cx, arms[0].body);
+ if !utils::usage::contains_return_break_continue_macro(cx, arms[1].body);
then {
let capture_mut = if bind_annotation == &BindingAnnotation::Mutable { "mut " } else { "" };
let some_body = extract_body_from_arm(&arms[0])?;
@@ -203,7 +203,7 @@ impl<'tcx> LateLintPass<'tcx> for OptionIfLetElse {
span_lint_and_sugg(
cx,
OPTION_IF_LET_ELSE,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
format!("use Option::{} instead of an if let/else", detection.method_sugg).as_str(),
"try",
format!(
diff --git a/src/tools/clippy/clippy_lints/src/overflow_check_conditional.rs b/src/tools/clippy/clippy_lints/src/overflow_check_conditional.rs
index 3c041bac234a5..555f4235e9a5d 100644
--- a/src/tools/clippy/clippy_lints/src/overflow_check_conditional.rs
+++ b/src/tools/clippy/clippy_lints/src/overflow_check_conditional.rs
@@ -41,13 +41,13 @@ impl<'tcx> LateLintPass<'tcx> for OverflowCheckConditional {
then {
if let BinOpKind::Lt = op.node {
if let BinOpKind::Add = op2.node {
- span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
+ span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, cx.tcx.hir().span(expr.hir_id),
"you are trying to use classic C overflow conditions that will fail in Rust");
}
}
if let BinOpKind::Gt = op.node {
if let BinOpKind::Sub = op2.node {
- span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
+ span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, cx.tcx.hir().span(expr.hir_id),
"you are trying to use classic C underflow conditions that will fail in Rust");
}
}
@@ -66,13 +66,13 @@ impl<'tcx> LateLintPass<'tcx> for OverflowCheckConditional {
then {
if let BinOpKind::Gt = op.node {
if let BinOpKind::Add = op2.node {
- span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
+ span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, cx.tcx.hir().span(expr.hir_id),
"you are trying to use classic C overflow conditions that will fail in Rust");
}
}
if let BinOpKind::Lt = op.node {
if let BinOpKind::Sub = op2.node {
- span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
+ span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, cx.tcx.hir().span(expr.hir_id),
"you are trying to use classic C underflow conditions that will fail in Rust");
}
}
diff --git a/src/tools/clippy/clippy_lints/src/panic_in_result_fn.rs b/src/tools/clippy/clippy_lints/src/panic_in_result_fn.rs
index 207423a186149..e0b5d6754da17 100644
--- a/src/tools/clippy/clippy_lints/src/panic_in_result_fn.rs
+++ b/src/tools/clippy/clippy_lints/src/panic_in_result_fn.rs
@@ -40,10 +40,10 @@ impl<'tcx> LateLintPass<'tcx> for PanicInResultFn {
fn_kind: FnKind<'tcx>,
_: &'tcx hir::FnDecl<'tcx>,
body: &'tcx hir::Body<'tcx>,
- span: Span,
hir_id: hir::HirId,
) {
if !matches!(fn_kind, FnKind::Closure) && is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym::result_type) {
+ let span = cx.tcx.hir().span_with_body(hir_id);
lint_impl_body(cx, span, body);
}
}
@@ -51,6 +51,7 @@ impl<'tcx> LateLintPass<'tcx> for PanicInResultFn {
fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, body: &'tcx hir::Body<'tcx>) {
let panics = find_macro_calls(
+ cx.tcx,
&[
"unimplemented",
"unreachable",
diff --git a/src/tools/clippy/clippy_lints/src/panic_unimplemented.rs b/src/tools/clippy/clippy_lints/src/panic_unimplemented.rs
index 359620cc07975..15a62293fc0ba 100644
--- a/src/tools/clippy/clippy_lints/src/panic_unimplemented.rs
+++ b/src/tools/clippy/clippy_lints/src/panic_unimplemented.rs
@@ -74,35 +74,37 @@ declare_lint_pass!(PanicUnimplemented => [UNIMPLEMENTED, UNREACHABLE, TODO, PANI
impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if match_panic_call(cx, expr).is_some() {
- let span = get_outer_span(expr);
- if is_expn_of(expr.span, "unimplemented").is_some() {
+ let span = get_outer_span(cx, expr);
+ let expr_span = cx.tcx.hir().span(expr.hir_id);
+ if is_expn_of(expr_span, "unimplemented").is_some() {
span_lint(
cx,
UNIMPLEMENTED,
span,
"`unimplemented` should not be present in production code",
);
- } else if is_expn_of(expr.span, "todo").is_some() {
+ } else if is_expn_of(expr_span, "todo").is_some() {
span_lint(cx, TODO, span, "`todo` should not be present in production code");
- } else if is_expn_of(expr.span, "unreachable").is_some() {
+ } else if is_expn_of(expr_span, "unreachable").is_some() {
span_lint(cx, UNREACHABLE, span, "usage of the `unreachable!` macro");
- } else if is_expn_of(expr.span, "panic").is_some() {
+ } else if is_expn_of(expr_span, "panic").is_some() {
span_lint(cx, PANIC, span, "`panic` should not be present in production code");
}
}
}
}
-fn get_outer_span(expr: &Expr<'_>) -> Span {
+fn get_outer_span(cx: &LateContext<'_>, expr: &Expr<'_>) -> Span {
+ let expr_span = cx.tcx.hir().span(expr.hir_id);
if_chain! {
- if expr.span.from_expansion();
- let first = expr.span.ctxt().outer_expn_data();
+ if expr_span.from_expansion();
+ let first = expr_span.ctxt().outer_expn_data();
if first.call_site.from_expansion();
let second = first.call_site.ctxt().outer_expn_data();
then {
second.call_site
} else {
- expr.span
+ expr_span
}
}
}
diff --git a/src/tools/clippy/clippy_lints/src/partialeq_ne_impl.rs b/src/tools/clippy/clippy_lints/src/partialeq_ne_impl.rs
index aca1ed5ca6563..1268bb83a8efe 100644
--- a/src/tools/clippy/clippy_lints/src/partialeq_ne_impl.rs
+++ b/src/tools/clippy/clippy_lints/src/partialeq_ne_impl.rs
@@ -46,7 +46,7 @@ impl<'tcx> LateLintPass<'tcx> for PartialEqNeImpl {
cx,
PARTIALEQ_NE_IMPL,
impl_item.id.hir_id(),
- impl_item.span,
+ cx.tcx.hir().span_with_body(impl_item.id.hir_id()),
"re-implementing `PartialEq::ne` is unnecessary",
);
}
diff --git a/src/tools/clippy/clippy_lints/src/pass_by_ref_or_value.rs b/src/tools/clippy/clippy_lints/src/pass_by_ref_or_value.rs
index ff700aa514607..162273b570d17 100644
--- a/src/tools/clippy/clippy_lints/src/pass_by_ref_or_value.rs
+++ b/src/tools/clippy/clippy_lints/src/pass_by_ref_or_value.rs
@@ -122,7 +122,7 @@ impl<'tcx> PassByRefOrValue {
for (index, (input, &ty)) in decl.inputs.iter().zip(fn_sig.inputs()).enumerate() {
// All spans generated from a proc-macro invocation are the same...
match span {
- Some(s) if s == input.span => return,
+ Some(s) if s == cx.tcx.hir().span(input.hir_id) => return,
_ => (),
}
@@ -147,12 +147,14 @@ impl<'tcx> PassByRefOrValue {
let value_type = if is_self_ty(decl_ty) {
"self".into()
} else {
- snippet(cx, decl_ty.span, "_").into()
+ let decl_ty_span = cx.tcx.hir().span(decl_ty.hir_id);
+ snippet(cx, decl_ty_span, "_").into()
};
+ let input_span = cx.tcx.hir().span(input.hir_id);
span_lint_and_sugg(
cx,
TRIVIALLY_COPY_PASS_BY_REF,
- input.span,
+ input_span,
&format!("this argument ({} byte) is passed by reference, but would be more efficient if passed by value (limit: {} byte)", size, self.ref_min_size),
"consider passing by value instead",
value_type,
@@ -178,13 +180,14 @@ impl<'tcx> PassByRefOrValue {
if let Some(size) = cx.layout_of(ty).ok().map(|l| l.size.bytes());
if size > self.value_max_size;
then {
+ let input_span = cx.tcx.hir().span(input.hir_id);
span_lint_and_sugg(
cx,
LARGE_TYPES_PASSED_BY_VALUE,
- input.span,
+ input_span,
&format!("this argument ({} byte) is passed by value, but might be more efficient if passed by reference (limit: {} byte)", size, self.value_max_size),
"consider passing by reference instead",
- format!("&{}", snippet(cx, input.span, "_")),
+ format!("&{}", snippet(cx, input_span, "_")),
Applicability::MaybeIncorrect,
);
}
@@ -201,7 +204,8 @@ impl_lint_pass!(PassByRefOrValue => [TRIVIALLY_COPY_PASS_BY_REF, LARGE_TYPES_PAS
impl<'tcx> LateLintPass<'tcx> for PassByRefOrValue {
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'_>) {
- if item.span.from_expansion() {
+ let item_span = cx.tcx.hir().span(item.hir_id());
+ if item_span.from_expansion() {
return;
}
@@ -216,9 +220,9 @@ impl<'tcx> LateLintPass<'tcx> for PassByRefOrValue {
kind: FnKind<'tcx>,
decl: &'tcx FnDecl<'_>,
_body: &'tcx Body<'_>,
- span: Span,
hir_id: HirId,
) {
+ let span = cx.tcx.hir().span(hir_id);
if span.from_expansion() {
return;
}
diff --git a/src/tools/clippy/clippy_lints/src/pattern_type_mismatch.rs b/src/tools/clippy/clippy_lints/src/pattern_type_mismatch.rs
index 5539331d0460b..a24a30cf23568 100644
--- a/src/tools/clippy/clippy_lints/src/pattern_type_mismatch.rs
+++ b/src/tools/clippy/clippy_lints/src/pattern_type_mismatch.rs
@@ -89,7 +89,8 @@ impl<'tcx> LateLintPass<'tcx> for PatternTypeMismatch {
if let Some(init) = &local.init {
if let Some(init_ty) = cx.typeck_results().node_type_opt(init.hir_id) {
let pat = &local.pat;
- if in_external_macro(cx.sess(), pat.span) {
+ let pat_span = cx.tcx.hir().span(pat.hir_id);
+ if in_external_macro(cx.sess(), pat_span) {
return;
}
let deref_possible = match local.source {
@@ -109,7 +110,8 @@ impl<'tcx> LateLintPass<'tcx> for PatternTypeMismatch {
if let Some(expr_ty) = cx.typeck_results().node_type_opt(expr.hir_id) {
'pattern_checks: for arm in arms {
let pat = &arm.pat;
- if in_external_macro(cx.sess(), pat.span) {
+ let pat_span = cx.tcx.hir().span(pat.hir_id);
+ if in_external_macro(cx.sess(), pat_span) {
continue 'pattern_checks;
}
if apply_lint(cx, pat, expr_ty, DerefPossible::Possible) {
@@ -129,7 +131,6 @@ impl<'tcx> LateLintPass<'tcx> for PatternTypeMismatch {
_: intravisit::FnKind<'tcx>,
_: &'tcx FnDecl<'_>,
body: &'tcx Body<'_>,
- _: Span,
hir_id: HirId,
) {
if let Some(fn_sig) = cx.typeck_results().liberated_fn_sigs().get(hir_id) {
@@ -194,7 +195,8 @@ fn find_first_mismatch<'tcx>(
if let TyKind::Ref(_, _, mutability) = *ty.kind() {
if is_non_ref_pattern(&pat.kind) {
- return Some((pat.span, mutability, level));
+ let pat_span = cx.tcx.hir().span(pat.hir_id);
+ return Some((pat_span, mutability, level));
}
}
diff --git a/src/tools/clippy/clippy_lints/src/ptr.rs b/src/tools/clippy/clippy_lints/src/ptr.rs
index 6ea2d8b06d81c..1758d36c09930 100644
--- a/src/tools/clippy/clippy_lints/src/ptr.rs
+++ b/src/tools/clippy/clippy_lints/src/ptr.rs
@@ -157,7 +157,7 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
span_lint(
cx,
CMP_NULL,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"comparing with null is better expressed by the `.is_null()` method",
);
}
@@ -186,13 +186,13 @@ fn check_fn(cx: &LateContext<'_>, decl: &FnDecl<'_>, fn_id: HirId, opt_body_id:
span_lint_and_then(
cx,
PTR_ARG,
- arg.span,
+ cx.tcx.hir().span(arg.hir_id),
"writing `&Vec<_>` instead of `&[_]` involves one more reference and cannot be used \
with non-Vec-based slices",
|diag| {
if let Some(ref snippet) = get_only_generic_arg_snippet(cx, arg) {
diag.span_suggestion(
- arg.span,
+ cx.tcx.hir().span(arg.hir_id),
"change this to",
format!("&[{}]", snippet),
Applicability::Unspecified,
@@ -216,10 +216,15 @@ fn check_fn(cx: &LateContext<'_>, decl: &FnDecl<'_>, fn_id: HirId, opt_body_id:
span_lint_and_then(
cx,
PTR_ARG,
- arg.span,
+ cx.tcx.hir().span(arg.hir_id),
"writing `&String` instead of `&str` involves a new object where a slice will do",
|diag| {
- diag.span_suggestion(arg.span, "change this to", "&str".into(), Applicability::Unspecified);
+ diag.span_suggestion(
+ cx.tcx.hir().span(arg.hir_id),
+ "change this to",
+ "&str".into(),
+ Applicability::Unspecified,
+ );
for (clonespan, suggestion) in spans {
diag.span_suggestion_short(
clonespan,
@@ -238,11 +243,11 @@ fn check_fn(cx: &LateContext<'_>, decl: &FnDecl<'_>, fn_id: HirId, opt_body_id:
span_lint_and_then(
cx,
PTR_ARG,
- arg.span,
+ cx.tcx.hir().span(arg.hir_id),
"writing `&PathBuf` instead of `&Path` involves a new object where a slice will do",
|diag| {
diag.span_suggestion(
- arg.span,
+ cx.tcx.hir().span(arg.hir_id),
"change this to",
"&Path".into(),
Applicability::Unspecified,
@@ -272,12 +277,12 @@ fn check_fn(cx: &LateContext<'_>, decl: &FnDecl<'_>, fn_id: HirId, opt_body_id:
_ => None,
});
then {
- let replacement = snippet_opt(cx, inner.span);
+ let replacement = snippet_opt(cx, cx.tcx.hir().span(inner.hir_id));
if let Some(r) = replacement {
span_lint_and_sugg(
cx,
PTR_ARG,
- arg.span,
+ cx.tcx.hir().span(arg.hir_id),
"using a reference to `Cow` is not recommended",
"change this to",
"&".to_owned() + &r,
@@ -291,12 +296,12 @@ fn check_fn(cx: &LateContext<'_>, decl: &FnDecl<'_>, fn_id: HirId, opt_body_id:
}
if let FnRetTy::Return(ref ty) = decl.output {
- if let Some((out, Mutability::Mut, _)) = get_rptr_lm(ty) {
+ if let Some((out, Mutability::Mut, _)) = get_rptr_lm(cx, ty) {
let mut immutables = vec![];
for (_, ref mutbl, ref argspan) in decl
.inputs
.iter()
- .filter_map(|ty| get_rptr_lm(ty))
+ .filter_map(|ty| get_rptr_lm(cx, ty))
.filter(|&(lt, _, _)| lt.name == out.name)
{
if *mutbl == Mutability::Mut {
@@ -310,7 +315,7 @@ fn check_fn(cx: &LateContext<'_>, decl: &FnDecl<'_>, fn_id: HirId, opt_body_id:
span_lint_and_then(
cx,
MUT_FROM_REF,
- ty.span,
+ cx.tcx.hir().span(ty.hir_id),
"mutable borrow from immutable input(s)",
|diag| {
let ms = MultiSpan::from_spans(immutables);
@@ -331,16 +336,16 @@ fn get_only_generic_arg_snippet(cx: &LateContext<'_>, arg: &Ty<'_>) -> Option(ty: &'tcx Ty<'tcx>) -> Option<(&'tcx Lifetime, Mutability, Span)> {
+fn get_rptr_lm<'tcx>(cx: &LateContext<'_>, ty: &'tcx Ty<'tcx>) -> Option<(&'tcx Lifetime, Mutability, Span)> {
if let TyKind::Rptr(ref lt, ref m) = ty.kind {
- Some((lt, m.mutbl, ty.span))
+ Some((lt, m.mutbl, cx.tcx.hir().span(ty.hir_id)))
} else {
None
}
diff --git a/src/tools/clippy/clippy_lints/src/ptr_eq.rs b/src/tools/clippy/clippy_lints/src/ptr_eq.rs
index 3be792ce5e4fa..01babb3dd2be7 100644
--- a/src/tools/clippy/clippy_lints/src/ptr_eq.rs
+++ b/src/tools/clippy/clippy_lints/src/ptr_eq.rs
@@ -40,7 +40,7 @@ static LINT_MSG: &str = "use `std::ptr::eq` when comparing raw pointers";
impl LateLintPass<'_> for PtrEq {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
- if utils::in_macro(expr.span) {
+ if utils::in_macro(cx.tcx.hir().span(expr.hir_id)) {
return;
}
@@ -54,13 +54,13 @@ impl LateLintPass<'_> for PtrEq {
if_chain! {
if let Some(left_var) = expr_as_cast_to_raw_pointer(cx, left);
if let Some(right_var) = expr_as_cast_to_raw_pointer(cx, right);
- if let Some(left_snip) = utils::snippet_opt(cx, left_var.span);
- if let Some(right_snip) = utils::snippet_opt(cx, right_var.span);
+ if let Some(left_snip) = utils::snippet_opt(cx, cx.tcx.hir().span(left_var.hir_id));
+ if let Some(right_snip) = utils::snippet_opt(cx, cx.tcx.hir().span(right_var.hir_id));
then {
utils::span_lint_and_sugg(
cx,
PTR_EQ,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
LINT_MSG,
"try",
format!("std::ptr::eq({}, {})", left_snip, right_snip),
diff --git a/src/tools/clippy/clippy_lints/src/ptr_offset_with_cast.rs b/src/tools/clippy/clippy_lints/src/ptr_offset_with_cast.rs
index e0996804a5934..497d967e47305 100644
--- a/src/tools/clippy/clippy_lints/src/ptr_offset_with_cast.rs
+++ b/src/tools/clippy/clippy_lints/src/ptr_offset_with_cast.rs
@@ -63,14 +63,14 @@ impl<'tcx> LateLintPass<'tcx> for PtrOffsetWithCast {
span_lint_and_sugg(
cx,
PTR_OFFSET_WITH_CAST,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
&msg,
"try",
sugg,
Applicability::MachineApplicable,
);
} else {
- span_lint(cx, PTR_OFFSET_WITH_CAST, expr.span, &msg);
+ span_lint(cx, PTR_OFFSET_WITH_CAST, cx.tcx.hir().span(expr.hir_id), &msg);
}
}
}
@@ -120,8 +120,8 @@ fn build_suggestion<'tcx>(
receiver_expr: &Expr<'_>,
cast_lhs_expr: &Expr<'_>,
) -> Option {
- let receiver = snippet_opt(cx, receiver_expr.span)?;
- let cast_lhs = snippet_opt(cx, cast_lhs_expr.span)?;
+ let receiver = snippet_opt(cx, cx.tcx.hir().span(receiver_expr.hir_id))?;
+ let cast_lhs = snippet_opt(cx, cx.tcx.hir().span(cast_lhs_expr.hir_id))?;
Some(format!("{}.{}({})", receiver, method.suggestion(), cast_lhs))
}
diff --git a/src/tools/clippy/clippy_lints/src/question_mark.rs b/src/tools/clippy/clippy_lints/src/question_mark.rs
index 6c480d48c7561..11a79229aabc1 100644
--- a/src/tools/clippy/clippy_lints/src/question_mark.rs
+++ b/src/tools/clippy/clippy_lints/src/question_mark.rs
@@ -83,7 +83,7 @@ impl QuestionMark {
span_lint_and_sugg(
cx,
QUESTION_MARK,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"this block may be rewritten with the `?` operator",
"replace it with",
replacement_str,
@@ -115,7 +115,7 @@ impl QuestionMark {
if Self::expression_returns_none(cx, arms[1].body);
then {
let mut applicability = Applicability::MachineApplicable;
- let receiver_str = snippet_with_applicability(cx, subject.span, "..", &mut applicability);
+ let receiver_str = snippet_with_applicability(cx, cx.tcx.hir().span(subject.hir_id), "..", &mut applicability);
let replacement = format!(
"{}{}?",
receiver_str,
@@ -125,7 +125,7 @@ impl QuestionMark {
span_lint_and_sugg(
cx,
QUESTION_MARK,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"this if-let-else may be rewritten with the `?` operator",
"replace it with",
replacement,
@@ -138,7 +138,7 @@ impl QuestionMark {
fn moves_by_default(cx: &LateContext<'_>, expression: &Expr<'_>) -> bool {
let expr_ty = cx.typeck_results().expr_ty(expression);
- !expr_ty.is_copy_modulo_regions(cx.tcx.at(expression.span), cx.param_env)
+ !expr_ty.is_copy_modulo_regions(cx.tcx.at(cx.tcx.hir().span(expression.hir_id)), cx.param_env)
}
fn is_option(cx: &LateContext<'_>, expression: &Expr<'_>) -> bool {
diff --git a/src/tools/clippy/clippy_lints/src/ranges.rs b/src/tools/clippy/clippy_lints/src/ranges.rs
index 59503817c0fcc..cb91c48b17a7d 100644
--- a/src/tools/clippy/clippy_lints/src/ranges.rs
+++ b/src/tools/clippy/clippy_lints/src/ranges.rs
@@ -186,7 +186,7 @@ impl<'tcx> LateLintPass<'tcx> for Ranges {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
match expr.kind {
ExprKind::MethodCall(ref path, _, ref args, _) => {
- check_range_zip_with_len(cx, path, args, expr.span);
+ check_range_zip_with_len(cx, path, args, cx.tcx.hir().span(expr.hir_id));
},
ExprKind::Binary(ref op, ref l, ref r) => {
if meets_msrv(self.msrv.as_ref(), &MANUAL_RANGE_CONTAINS_MSRV) {
@@ -208,7 +208,7 @@ fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'
return;
}
- let span = expr.span;
+ let span = cx.tcx.hir().span(expr.hir_id);
let combine_and = match op {
BinOpKind::And | BinOpKind::BitAnd => true,
BinOpKind::Or | BinOpKind::BitOr => false,
@@ -299,11 +299,11 @@ fn check_range_bounds(cx: &LateContext<'_>, ex: &Expr<'_>) -> Option<(Constant,
};
if let Some(id) = match_ident(l) {
if let Some((c, _)) = constant(cx, cx.typeck_results(), r) {
- return Some((c, id, l.span, r.span, ordering, inclusive));
+ return Some((c, id, cx.tcx.hir().span(l.hir_id), cx.tcx.hir().span(r.hir_id), ordering, inclusive));
}
} else if let Some(id) = match_ident(r) {
if let Some((c, _)) = constant(cx, cx.typeck_results(), l) {
- return Some((c, id, r.span, l.span, ordering.reverse(), inclusive));
+ return Some((c, id, cx.tcx.hir().span(r.hir_id), cx.tcx.hir().span(l.hir_id), ordering.reverse(), inclusive));
}
}
}
@@ -345,7 +345,7 @@ fn check_range_zip_with_len(cx: &LateContext<'_>, path: &PathSegment<'_>, args:
RANGE_ZIP_WITH_LEN,
span,
&format!("it is more idiomatic to use `{}.iter().enumerate()`",
- snippet(cx, iter_args[0].span, "_"))
+ snippet(cx, cx.tcx.hir().span(iter_args[0].hir_id), "_"))
);
}
}
@@ -362,13 +362,13 @@ fn check_exclusive_range_plus_one(cx: &LateContext<'_>, expr: &Expr<'_>) {
}) = higher::range(expr);
if let Some(y) = y_plus_one(cx, end);
then {
- let span = if expr.span.from_expansion() {
- expr.span
+ let span = if cx.tcx.hir().span(expr.hir_id).from_expansion() {
+ cx.tcx.hir().span(expr.hir_id)
.ctxt()
.outer_expn_data()
.call_site
} else {
- expr.span
+ cx.tcx.hir().span(expr.hir_id)
};
span_lint_and_then(
cx,
@@ -410,13 +410,13 @@ fn check_inclusive_range_minus_one(cx: &LateContext<'_>, expr: &Expr<'_>) {
span_lint_and_then(
cx,
RANGE_MINUS_ONE,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"an exclusive range would be more readable",
|diag| {
let start = start.map_or(String::new(), |x| Sugg::hir(cx, x, "x").to_string());
let end = Sugg::hir(cx, y, "y");
diag.span_suggestion(
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"use",
format!("{}..{}", start, end),
Applicability::MachineApplicable, // snippet
@@ -441,7 +441,7 @@ fn check_reversed_empty_range(cx: &LateContext<'_>, expr: &Expr<'_>) {
fn is_for_loop_arg(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
let mut cur_expr = expr;
while let Some(parent_expr) = get_parent_expr(cx, cur_expr) {
- match higher::for_loop(parent_expr) {
+ match higher::for_loop(cx, parent_expr) {
Some((_, args, _, _)) if args.hir_id == expr.hir_id => return true,
_ => cur_expr = parent_expr,
}
@@ -468,32 +468,34 @@ fn check_reversed_empty_range(cx: &LateContext<'_>, expr: &Expr<'_>) {
then {
if inside_indexing_expr(cx, expr) {
// Avoid linting `N..N` as it has proven to be useful, see #5689 and #5628 ...
+ let expr_span = cx.tcx.hir().span(expr.hir_id);
if ordering != Ordering::Equal {
span_lint(
cx,
REVERSED_EMPTY_RANGES,
- expr.span,
+ expr_span,
"this range is reversed and using it to index a slice will panic at run-time",
);
}
// ... except in for loop arguments for backwards compatibility with `reverse_range_loop`
} else if ordering != Ordering::Equal || is_for_loop_arg(cx, expr) {
+ let expr_span = cx.tcx.hir().span(expr.hir_id);
span_lint_and_then(
cx,
REVERSED_EMPTY_RANGES,
- expr.span,
+ expr_span,
"this range is empty so it will yield no values",
|diag| {
if ordering != Ordering::Equal {
- let start_snippet = snippet(cx, start.span, "_");
- let end_snippet = snippet(cx, end.span, "_");
+ let start_snippet = snippet(cx, cx.tcx.hir().span(start.hir_id), "_");
+ let end_snippet = snippet(cx, cx.tcx.hir().span(end.hir_id), "_");
let dots = match limits {
RangeLimits::HalfOpen => "..",
RangeLimits::Closed => "..="
};
diag.span_suggestion(
- expr.span,
+ expr_span,
"consider using the following if you are attempting to iterate over this \
range in reverse",
format!("({}{}{}).rev()", end_snippet, dots, start_snippet),
diff --git a/src/tools/clippy/clippy_lints/src/redundant_clone.rs b/src/tools/clippy/clippy_lints/src/redundant_clone.rs
index f90d48205633e..48eb9d9bd44ef 100644
--- a/src/tools/clippy/clippy_lints/src/redundant_clone.rs
+++ b/src/tools/clippy/clippy_lints/src/redundant_clone.rs
@@ -16,7 +16,7 @@ use rustc_middle::mir::{
use rustc_middle::ty::{self, fold::TypeVisitor, Ty};
use rustc_mir::dataflow::{Analysis, AnalysisDomain, GenKill, GenKillAnalysis, ResultsCursor};
use rustc_session::{declare_lint_pass, declare_tool_lint};
-use rustc_span::source_map::{BytePos, Span};
+use rustc_span::source_map::BytePos;
use rustc_span::sym;
use std::convert::TryFrom;
use std::ops::ControlFlow;
@@ -75,7 +75,6 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClone {
_: FnKind<'tcx>,
_: &'tcx FnDecl<'_>,
body: &'tcx Body<'_>,
- _: Span,
_: HirId,
) {
let def_id = cx.tcx.hir().body_owner_def_id(body.id());
diff --git a/src/tools/clippy/clippy_lints/src/redundant_closure_call.rs b/src/tools/clippy/clippy_lints/src/redundant_closure_call.rs
index f398b3fff25a3..db4d7011b1a6e 100644
--- a/src/tools/clippy/clippy_lints/src/redundant_closure_call.rs
+++ b/src/tools/clippy/clippy_lints/src/redundant_closure_call.rs
@@ -146,7 +146,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
span_lint(
cx,
REDUNDANT_CLOSURE_CALL,
- second.span,
+ cx.tcx.hir().span(second.hir_id),
"closure called just once immediately after it was declared",
);
}
diff --git a/src/tools/clippy/clippy_lints/src/redundant_pub_crate.rs b/src/tools/clippy/clippy_lints/src/redundant_pub_crate.rs
index c876bae2303ad..9ed2882d7a40e 100644
--- a/src/tools/clippy/clippy_lints/src/redundant_pub_crate.rs
+++ b/src/tools/clippy/clippy_lints/src/redundant_pub_crate.rs
@@ -44,7 +44,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate {
if let VisibilityKind::Crate { .. } = item.vis.node {
if !cx.access_levels.is_exported(item.hir_id()) {
if let Some(false) = self.is_exported.last() {
- let span = item.span.with_hi(item.ident.span.hi());
+ let span = cx.tcx.hir().span(item.hir_id()).with_hi(item.ident.span.hi());
let descr = cx.tcx.def_kind(item.def_id).descr(item.def_id.to_def_id());
span_lint_and_then(
cx,
diff --git a/src/tools/clippy/clippy_lints/src/redundant_slicing.rs b/src/tools/clippy/clippy_lints/src/redundant_slicing.rs
index e5ced13514f79..e6657e2bd639d 100644
--- a/src/tools/clippy/clippy_lints/src/redundant_slicing.rs
+++ b/src/tools/clippy/clippy_lints/src/redundant_slicing.rs
@@ -39,7 +39,7 @@ declare_lint_pass!(RedundantSlicing => [REDUNDANT_SLICING]);
impl LateLintPass<'_> for RedundantSlicing {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
- if in_external_macro(cx.sess(), expr.span) {
+ if in_external_macro(cx.sess(), cx.tcx.hir().span(expr.hir_id)) {
return;
}
@@ -50,12 +50,12 @@ impl LateLintPass<'_> for RedundantSlicing {
if TyS::same_type(cx.typeck_results().expr_ty(expr), cx.typeck_results().expr_ty(indexed));
then {
let mut app = Applicability::MachineApplicable;
- let hint = snippet_with_applicability(cx, indexed.span, "..", &mut app).into_owned();
+ let hint = snippet_with_applicability(cx, cx.tcx.hir().span(indexed.hir_id), "..", &mut app).into_owned();
span_lint_and_sugg(
cx,
REDUNDANT_SLICING,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"redundant slicing of the whole range",
"use the original slice instead",
hint,
diff --git a/src/tools/clippy/clippy_lints/src/ref_option_ref.rs b/src/tools/clippy/clippy_lints/src/ref_option_ref.rs
index 8cd6692ce03a0..2c2374344d141 100644
--- a/src/tools/clippy/clippy_lints/src/ref_option_ref.rs
+++ b/src/tools/clippy/clippy_lints/src/ref_option_ref.rs
@@ -52,13 +52,15 @@ impl<'tcx> LateLintPass<'tcx> for RefOptionRef {
if let TyKind::Rptr(_, _) = inner_ty.kind;
then {
+ let ty_span = cx.tcx.hir().span(ty.hir_id);
+ let inner_ty_span = cx.tcx.hir().span(inner_ty.hir_id);
span_lint_and_sugg(
cx,
REF_OPTION_REF,
- ty.span,
+ ty_span,
"since `&` implements the `Copy` trait, `&Option<&T>` can be simplified to `Option<&T>`",
"try",
- format!("Option<{}>", &snippet(cx, inner_ty.span, "..")),
+ format!("Option<{}>", &snippet(cx, inner_ty_span, "..")),
Applicability::MaybeIncorrect,
);
}
diff --git a/src/tools/clippy/clippy_lints/src/regex.rs b/src/tools/clippy/clippy_lints/src/regex.rs
index 1edea61314893..af783d886c869 100644
--- a/src/tools/clippy/clippy_lints/src/regex.rs
+++ b/src/tools/clippy/clippy_lints/src/regex.rs
@@ -156,14 +156,14 @@ fn check_regex<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, utf8: bool) {
match parser.parse(r) {
Ok(r) => {
if let Some(repl) = is_trivial_regex(&r) {
- span_lint_and_help(cx, TRIVIAL_REGEX, expr.span, "trivial regex", None, repl);
+ span_lint_and_help(cx, TRIVIAL_REGEX, cx.tcx.hir().span(expr.hir_id), "trivial regex", None, repl);
}
},
Err(regex_syntax::Error::Parse(e)) => {
span_lint(
cx,
INVALID_REGEX,
- str_span(expr.span, *e.span(), offset),
+ str_span(cx.tcx.hir().span(expr.hir_id), *e.span(), offset),
&format!("regex syntax error: {}", e.kind()),
);
},
@@ -171,12 +171,12 @@ fn check_regex<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, utf8: bool) {
span_lint(
cx,
INVALID_REGEX,
- str_span(expr.span, *e.span(), offset),
+ str_span(cx.tcx.hir().span(expr.hir_id), *e.span(), offset),
&format!("regex syntax error: {}", e.kind()),
);
},
Err(e) => {
- span_lint(cx, INVALID_REGEX, expr.span, &format!("regex syntax error: {}", e));
+ span_lint(cx, INVALID_REGEX, cx.tcx.hir().span(expr.hir_id), &format!("regex syntax error: {}", e));
},
}
}
@@ -184,14 +184,14 @@ fn check_regex<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, utf8: bool) {
match parser.parse(&r) {
Ok(r) => {
if let Some(repl) = is_trivial_regex(&r) {
- span_lint_and_help(cx, TRIVIAL_REGEX, expr.span, "trivial regex", None, repl);
+ span_lint_and_help(cx, TRIVIAL_REGEX, cx.tcx.hir().span(expr.hir_id), "trivial regex", None, repl);
}
},
Err(regex_syntax::Error::Parse(e)) => {
span_lint(
cx,
INVALID_REGEX,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
&format!("regex syntax error on position {}: {}", e.span().start.offset, e.kind()),
);
},
@@ -199,12 +199,12 @@ fn check_regex<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, utf8: bool) {
span_lint(
cx,
INVALID_REGEX,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
&format!("regex syntax error on position {}: {}", e.span().start.offset, e.kind()),
);
},
Err(e) => {
- span_lint(cx, INVALID_REGEX, expr.span, &format!("regex syntax error: {}", e));
+ span_lint(cx, INVALID_REGEX, cx.tcx.hir().span(expr.hir_id), &format!("regex syntax error: {}", e));
},
}
}
diff --git a/src/tools/clippy/clippy_lints/src/repeat_once.rs b/src/tools/clippy/clippy_lints/src/repeat_once.rs
index d34e744eb944c..4387f713afd99 100644
--- a/src/tools/clippy/clippy_lints/src/repeat_once.rs
+++ b/src/tools/clippy/clippy_lints/src/repeat_once.rs
@@ -43,37 +43,39 @@ impl<'tcx> LateLintPass<'tcx> for RepeatOnce {
if let ExprKind::MethodCall(path, _, [receiver, count], _) = &expr.kind;
if path.ident.name == sym!(repeat);
if let Some(Constant::Int(1)) = constant_context(cx, cx.typeck_results()).expr(&count);
- if !in_macro(receiver.span);
+ let receiver_span = cx.tcx.hir().span(receiver.hir_id);
+ if !in_macro(receiver_span);
then {
let ty = cx.typeck_results().expr_ty(&receiver).peel_refs();
+ let expr_span = cx.tcx.hir().span(expr.hir_id);
if ty.is_str() {
span_lint_and_sugg(
cx,
REPEAT_ONCE,
- expr.span,
+ expr_span,
"calling `repeat(1)` on str",
"consider using `.to_string()` instead",
- format!("{}.to_string()", snippet(cx, receiver.span, r#""...""#)),
+ format!("{}.to_string()", snippet(cx, receiver_span, r#""...""#)),
Applicability::MachineApplicable,
);
} else if ty.builtin_index().is_some() {
span_lint_and_sugg(
cx,
REPEAT_ONCE,
- expr.span,
+ expr_span,
"calling `repeat(1)` on slice",
"consider using `.to_vec()` instead",
- format!("{}.to_vec()", snippet(cx, receiver.span, r#""...""#)),
+ format!("{}.to_vec()", snippet(cx, receiver_span, r#""...""#)),
Applicability::MachineApplicable,
);
} else if is_type_diagnostic_item(cx, ty, sym::string_type) {
span_lint_and_sugg(
cx,
REPEAT_ONCE,
- expr.span,
+ expr_span,
"calling `repeat(1)` on a string literal",
"consider using `.clone()` instead",
- format!("{}.clone()", snippet(cx, receiver.span, r#""...""#)),
+ format!("{}.clone()", snippet(cx, receiver_span, r#""...""#)),
Applicability::MachineApplicable,
);
}
diff --git a/src/tools/clippy/clippy_lints/src/returns.rs b/src/tools/clippy/clippy_lints/src/returns.rs
index 40c0f1f45895b..c8a956f3184dc 100644
--- a/src/tools/clippy/clippy_lints/src/returns.rs
+++ b/src/tools/clippy/clippy_lints/src/returns.rs
@@ -87,33 +87,36 @@ impl<'tcx> LateLintPass<'tcx> for Return {
if let ExprKind::Path(qpath) = &retexpr.kind;
if match_qpath(qpath, &[&*ident.name.as_str()]);
if !last_statement_borrows(cx, initexpr);
- if !in_external_macro(cx.sess(), initexpr.span);
- if !in_external_macro(cx.sess(), retexpr.span);
- if !in_external_macro(cx.sess(), local.span);
- if !in_macro(local.span);
+ let initexpr_span = cx.tcx.hir().span(initexpr.hir_id);
+ if !in_external_macro(cx.sess(), initexpr_span);
+ let retexpr_span = cx.tcx.hir().span(retexpr.hir_id);
+ if !in_external_macro(cx.sess(), retexpr_span);
+ let local_span = cx.tcx.hir().span(local.hir_id);
+ if !in_external_macro(cx.sess(), local_span);
+ if !in_macro(local_span);
then {
span_lint_and_then(
cx,
LET_AND_RETURN,
- retexpr.span,
+ retexpr_span,
"returning the result of a `let` binding from a block",
|err| {
- err.span_label(local.span, "unnecessary `let` binding");
+ err.span_label(local_span, "unnecessary `let` binding");
- if let Some(mut snippet) = snippet_opt(cx, initexpr.span) {
+ if let Some(mut snippet) = snippet_opt(cx, initexpr_span) {
if !cx.typeck_results().expr_adjustments(&retexpr).is_empty() {
snippet.push_str(" as _");
}
err.multipart_suggestion(
"return the expression directly",
vec![
- (local.span, String::new()),
- (retexpr.span, snippet),
+ (local_span, String::new()),
+ (retexpr_span, snippet),
],
Applicability::MachineApplicable,
);
} else {
- err.span_help(initexpr.span, "this expression can be directly returned");
+ err.span_help(initexpr_span, "this expression can be directly returned");
}
},
);
@@ -127,7 +130,6 @@ impl<'tcx> LateLintPass<'tcx> for Return {
kind: FnKind<'tcx>,
_: &'tcx FnDecl<'tcx>,
body: &'tcx Body<'tcx>,
- _: Span,
_: HirId,
) {
match kind {
@@ -139,7 +141,7 @@ impl<'tcx> LateLintPass<'tcx> for Return {
} else {
RetReplacement::Empty
};
- check_final_expr(cx, &body.value, Some(body.value.span), replacement)
+ check_final_expr(cx, &body.value, Some(cx.tcx.hir().span(body.value.hir_id)), replacement)
},
FnKind::ItemFn(..) | FnKind::Method(..) => {
if let ExprKind::Block(ref block, _) = body.value.kind {
@@ -156,11 +158,12 @@ fn attr_is_cfg(attr: &Attribute) -> bool {
fn check_block_return<'tcx>(cx: &LateContext<'tcx>, block: &Block<'tcx>) {
if let Some(expr) = block.expr {
- check_final_expr(cx, expr, Some(expr.span), RetReplacement::Empty);
+ check_final_expr(cx, expr, Some(cx.tcx.hir().span(expr.hir_id)), RetReplacement::Empty);
} else if let Some(stmt) = block.stmts.iter().last() {
match stmt.kind {
StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => {
- check_final_expr(cx, expr, Some(stmt.span), RetReplacement::Empty);
+ let stmt_span = cx.tcx.hir().span(stmt.hir_id);
+ check_final_expr(cx, expr, Some(stmt_span), RetReplacement::Empty);
},
_ => (),
}
@@ -184,7 +187,7 @@ fn check_final_expr<'tcx>(
emit_return_lint(
cx,
span.expect("`else return` is not possible"),
- inner.as_ref().map(|i| i.span),
+ inner.as_ref().map(|i| cx.tcx.hir().span(i.hir_id)),
replacement,
);
}
@@ -209,7 +212,12 @@ fn check_final_expr<'tcx>(
ExprKind::Match(_, ref arms, source) => match source {
MatchSource::Normal => {
for arm in arms.iter() {
- check_final_expr(cx, &arm.body, Some(arm.body.span), RetReplacement::Block);
+ check_final_expr(
+ cx,
+ &arm.body,
+ Some(cx.tcx.hir().span(arm.body.hir_id)),
+ RetReplacement::Block,
+ );
}
},
MatchSource::IfLetDesugar {
diff --git a/src/tools/clippy/clippy_lints/src/self_assignment.rs b/src/tools/clippy/clippy_lints/src/self_assignment.rs
index e096c9aebc122..85368a839ff50 100644
--- a/src/tools/clippy/clippy_lints/src/self_assignment.rs
+++ b/src/tools/clippy/clippy_lints/src/self_assignment.rs
@@ -37,12 +37,12 @@ impl<'tcx> LateLintPass<'tcx> for SelfAssignment {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if let ExprKind::Assign(lhs, rhs, _) = &expr.kind {
if eq_expr_value(cx, lhs, rhs) {
- let lhs = snippet(cx, lhs.span, "");
- let rhs = snippet(cx, rhs.span, "");
+ let lhs = snippet(cx, cx.tcx.hir().span(lhs.hir_id), "");
+ let rhs = snippet(cx, cx.tcx.hir().span(rhs.hir_id), "");
span_lint(
cx,
SELF_ASSIGNMENT,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
&format!("self-assignment of `{}` to `{}`", rhs, lhs),
);
}
diff --git a/src/tools/clippy/clippy_lints/src/semicolon_if_nothing_returned.rs b/src/tools/clippy/clippy_lints/src/semicolon_if_nothing_returned.rs
index 839c995e52562..d823872704edc 100644
--- a/src/tools/clippy/clippy_lints/src/semicolon_if_nothing_returned.rs
+++ b/src/tools/clippy/clippy_lints/src/semicolon_if_nothing_returned.rs
@@ -37,11 +37,11 @@ declare_lint_pass!(SemicolonIfNothingReturned => [SEMICOLON_IF_NOTHING_RETURNED]
impl LateLintPass<'_> for SemicolonIfNothingReturned {
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
if_chain! {
- if !in_macro(block.span);
+ if !in_macro(cx.tcx.hir().span(block.hir_id));
if let Some(expr) = block.expr;
let t_expr = cx.typeck_results().expr_ty(expr);
if t_expr.is_unit();
- if let snippet = snippet_with_macro_callsite(cx, expr.span, "}");
+ if let snippet = snippet_with_macro_callsite(cx, cx.tcx.hir().span(expr.hir_id), "}");
if !snippet.ends_with('}');
then {
// filter out the desugared `for` loop
@@ -54,7 +54,7 @@ impl LateLintPass<'_> for SemicolonIfNothingReturned {
span_lint_and_sugg(
cx,
SEMICOLON_IF_NOTHING_RETURNED,
- expr.span.source_callsite(),
+ cx.tcx.hir().span(expr.hir_id).source_callsite(),
"consider adding a `;` to the last statement for consistent formatting",
"add a `;` here",
suggestion,
diff --git a/src/tools/clippy/clippy_lints/src/serde_api.rs b/src/tools/clippy/clippy_lints/src/serde_api.rs
index 90cf1b6c86135..7fdc2ab8b8bdf 100644
--- a/src/tools/clippy/clippy_lints/src/serde_api.rs
+++ b/src/tools/clippy/clippy_lints/src/serde_api.rs
@@ -35,8 +35,8 @@ impl<'tcx> LateLintPass<'tcx> for SerdeApi {
let mut seen_string = None;
for item in items {
match &*item.ident.as_str() {
- "visit_str" => seen_str = Some(item.span),
- "visit_string" => seen_string = Some(item.span),
+ "visit_str" => seen_str = Some(cx.tcx.hir().span_with_body(item.id.hir_id())),
+ "visit_string" => seen_string = Some(cx.tcx.hir().span_with_body(item.id.hir_id())),
_ => {},
}
}
diff --git a/src/tools/clippy/clippy_lints/src/shadow.rs b/src/tools/clippy/clippy_lints/src/shadow.rs
index 32f6bc74642ca..866d93802e39e 100644
--- a/src/tools/clippy/clippy_lints/src/shadow.rs
+++ b/src/tools/clippy/clippy_lints/src/shadow.rs
@@ -104,10 +104,9 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
_: FnKind<'tcx>,
decl: &'tcx FnDecl<'_>,
body: &'tcx Body<'_>,
- _: Span,
_: HirId,
) {
- if in_external_macro(cx.sess(), body.value.span) {
+ if in_external_macro(cx.sess(), cx.tcx.hir().span(body.value.hir_id)) {
return;
}
check_fn(cx, decl, body);
@@ -140,7 +139,8 @@ fn check_block<'tcx>(cx: &LateContext<'tcx>, block: &'tcx Block<'_>, bindings: &
}
fn check_local<'tcx>(cx: &LateContext<'tcx>, local: &'tcx Local<'_>, bindings: &mut Vec<(Symbol, Span)>) {
- if in_external_macro(cx.sess(), local.span) {
+ let span = cx.tcx.hir().span(local.hir_id);
+ if in_external_macro(cx.sess(), span) {
return;
}
if higher::is_from_for_desugar(local) {
@@ -150,7 +150,6 @@ fn check_local<'tcx>(cx: &LateContext<'tcx>, local: &'tcx Local<'_>, bindings: &
ref pat,
ref ty,
ref init,
- span,
..
} = *local;
if let Some(ref t) = *ty {
@@ -184,7 +183,7 @@ fn check_pat<'tcx>(
let mut new_binding = true;
for tup in bindings.iter_mut() {
if tup.0 == name {
- lint_shadow(cx, name, span, pat.span, init, tup.1);
+ lint_shadow(cx, name, span, cx.tcx.hir().span(pat.hir_id), init, tup.1);
tup.1 = ident.span;
new_binding = false;
break;
@@ -223,7 +222,7 @@ fn check_pat<'tcx>(
if let Some(init_tup) = init {
if let ExprKind::Tup(ref tup) = init_tup.kind {
for (i, p) in inner.iter().enumerate() {
- check_pat(cx, p, Some(&tup[i]), p.span, bindings);
+ check_pat(cx, p, Some(&tup[i]), cx.tcx.hir().span(p.hir_id), bindings);
}
} else {
for p in inner {
@@ -262,6 +261,7 @@ fn lint_shadow<'tcx>(
prev_span: Span,
) {
if let Some(expr) = init {
+ let expr_span = cx.tcx.hir().span(expr.hir_id);
if is_self_shadow(name, expr) {
span_lint_and_then(
cx,
@@ -270,7 +270,7 @@ fn lint_shadow<'tcx>(
&format!(
"`{}` is shadowed by itself in `{}`",
snippet(cx, pattern_span, "_"),
- snippet(cx, expr.span, "..")
+ snippet(cx, expr_span, "..")
),
|diag| {
diag.span_note(prev_span, "previous binding is here");
@@ -284,10 +284,10 @@ fn lint_shadow<'tcx>(
&format!(
"`{}` is shadowed by `{}` which reuses the original value",
snippet(cx, pattern_span, "_"),
- snippet(cx, expr.span, "..")
+ snippet(cx, expr_span, "..")
),
|diag| {
- diag.span_note(expr.span, "initialization happens here");
+ diag.span_note(expr_span, "initialization happens here");
diag.span_note(prev_span, "previous binding is here");
},
);
@@ -296,9 +296,12 @@ fn lint_shadow<'tcx>(
cx,
SHADOW_UNRELATED,
pattern_span,
- &format!("`{}` is being shadowed", snippet(cx, pattern_span, "_")),
+ &format!(
+ "`{}` is being shadowed",
+ snippet(cx, pattern_span, "_"),
+ ),
|diag| {
- diag.span_note(expr.span, "initialization happens here");
+ diag.span_note(expr_span, "initialization happens here");
diag.span_note(prev_span, "previous binding is here");
},
);
@@ -317,7 +320,7 @@ fn lint_shadow<'tcx>(
}
fn check_expr<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, bindings: &mut Vec<(Symbol, Span)>) {
- if in_external_macro(cx.sess(), expr.span) {
+ if in_external_macro(cx.sess(), cx.tcx.hir().span(expr.hir_id)) {
return;
}
match expr.kind {
@@ -344,13 +347,13 @@ fn check_expr<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, bindings: &mut
check_expr(cx, init, bindings);
let len = bindings.len();
for arm in arms {
- check_pat(cx, &arm.pat, Some(&**init), arm.pat.span, bindings);
+ check_pat(cx, &arm.pat, Some(&**init), cx.tcx.hir().span(arm.pat.hir_id), bindings);
// This is ugly, but needed to get the right type
if let Some(ref guard) = arm.guard {
match guard {
Guard::If(if_expr) => check_expr(cx, if_expr, bindings),
Guard::IfLet(guard_pat, guard_expr) => {
- check_pat(cx, guard_pat, Some(*guard_expr), guard_pat.span, bindings);
+ check_pat(cx, guard_pat, Some(*guard_expr), cx.tcx.hir().span(guard_pat.hir_id), bindings);
check_expr(cx, guard_expr, bindings);
},
}
diff --git a/src/tools/clippy/clippy_lints/src/size_of_in_element_count.rs b/src/tools/clippy/clippy_lints/src/size_of_in_element_count.rs
index 87e386baadc54..6f9b72e8ac393 100644
--- a/src/tools/clippy/clippy_lints/src/size_of_in_element_count.rs
+++ b/src/tools/clippy/clippy_lints/src/size_of_in_element_count.rs
@@ -138,7 +138,7 @@ impl<'tcx> LateLintPass<'tcx> for SizeOfInElementCount {
span_lint_and_help(
cx,
SIZE_OF_IN_ELEMENT_COUNT,
- count_expr.span,
+ cx.tcx.hir().span(count_expr.hir_id),
LINT_MSG,
None,
HELP_MSG
diff --git a/src/tools/clippy/clippy_lints/src/slow_vector_initialization.rs b/src/tools/clippy/clippy_lints/src/slow_vector_initialization.rs
index 96f6881556cf3..fb69dc422c8f7 100644
--- a/src/tools/clippy/clippy_lints/src/slow_vector_initialization.rs
+++ b/src/tools/clippy/clippy_lints/src/slow_vector_initialization.rs
@@ -176,9 +176,9 @@ impl SlowVectorInit {
) {
let len_expr = Sugg::hir(cx, vec_alloc.len_expr, "len");
- span_lint_and_then(cx, lint, slow_fill.span, msg, |diag| {
+ span_lint_and_then(cx, lint, cx.tcx.hir().span(slow_fill.hir_id), msg, |diag| {
diag.span_suggestion(
- vec_alloc.allocation_expr.span,
+ cx.tcx.hir().span(vec_alloc.allocation_expr.hir_id),
"consider replace allocation with",
format!("vec![0; {}]", len_expr),
Applicability::Unspecified,
diff --git a/src/tools/clippy/clippy_lints/src/stable_sort_primitive.rs b/src/tools/clippy/clippy_lints/src/stable_sort_primitive.rs
index 276a9338819d9..bfa8af909ad1d 100644
--- a/src/tools/clippy/clippy_lints/src/stable_sort_primitive.rs
+++ b/src/tools/clippy/clippy_lints/src/stable_sort_primitive.rs
@@ -110,7 +110,7 @@ impl LateLintPass<'_> for StableSortPrimitive {
span_lint_and_then(
cx,
STABLE_SORT_PRIMITIVE,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
format!(
"used `{}` on primitive type `{}`",
detection.method.stable_name(),
@@ -119,7 +119,7 @@ impl LateLintPass<'_> for StableSortPrimitive {
.as_str(),
|diag| {
diag.span_suggestion(
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"try",
format!(
"{}.{}({})",
diff --git a/src/tools/clippy/clippy_lints/src/strings.rs b/src/tools/clippy/clippy_lints/src/strings.rs
index 31dd5965473d3..0b11150c731a7 100644
--- a/src/tools/clippy/clippy_lints/src/strings.rs
+++ b/src/tools/clippy/clippy_lints/src/strings.rs
@@ -113,7 +113,7 @@ declare_lint_pass!(StringAdd => [STRING_ADD, STRING_ADD_ASSIGN]);
impl<'tcx> LateLintPass<'tcx> for StringAdd {
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
- if in_external_macro(cx.sess(), e.span) {
+ if in_external_macro(cx.sess(), cx.tcx.hir().span(e.hir_id)) {
return;
}
@@ -140,7 +140,7 @@ impl<'tcx> LateLintPass<'tcx> for StringAdd {
span_lint(
cx,
STRING_ADD,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
"you added something to a string. Consider using `String::push_str()` instead",
);
}
@@ -149,7 +149,7 @@ impl<'tcx> LateLintPass<'tcx> for StringAdd {
span_lint(
cx,
STRING_ADD_ASSIGN,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
"you assigned the result of adding something to this string. Consider using \
`String::push_str()` instead",
);
@@ -215,7 +215,7 @@ impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes {
// Find string::as_bytes
if let ExprKind::AddrOf(BorrowKind::Ref, _, ref args) = args[0].kind;
if let ExprKind::Index(ref left, ref right) = args.kind;
- let (method_names, expressions, _) = method_calls(left, 1);
+ let (method_names, expressions, _) = method_calls(cx, left, 1);
if method_names.len() == 1;
if expressions.len() == 1;
if expressions[0].len() == 1;
@@ -230,17 +230,17 @@ impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes {
let snippet_app = snippet_with_applicability(
cx,
- string_expression.span, "..",
+ cx.tcx.hir().span(string_expression.hir_id), "..",
&mut applicability,
);
span_lint_and_sugg(
cx,
STRING_FROM_UTF8_AS_BYTES,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
"calling a slice of `as_bytes()` with `from_utf8` should be not necessary",
"try",
- format!("Some(&{}[{}])", snippet_app, snippet(cx, right.span, "..")),
+ format!("Some(&{}[{}])", snippet_app, snippet(cx, cx.tcx.hir().span(right.hir_id), "..")),
applicability
)
}
@@ -252,16 +252,16 @@ impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes {
if let ExprKind::Lit(lit) = &args[0].kind;
if let LitKind::Str(lit_content, _) = &lit.node;
then {
- let callsite = snippet(cx, args[0].span.source_callsite(), r#""foo""#);
+ let callsite = snippet(cx, cx.tcx.hir().span(args[0].hir_id).source_callsite(), r#""foo""#);
let mut applicability = Applicability::MachineApplicable;
if callsite.starts_with("include_str!") {
span_lint_and_sugg(
cx,
STRING_LIT_AS_BYTES,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
"calling `as_bytes()` on `include_str!(..)`",
"consider using `include_bytes!(..)` instead",
- snippet_with_applicability(cx, args[0].span, r#""foo""#, &mut applicability).replacen(
+ snippet_with_applicability(cx, cx.tcx.hir().span(args[0].hir_id), r#""foo""#, &mut applicability).replacen(
"include_str",
"include_bytes",
1,
@@ -270,17 +270,17 @@ impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes {
);
} else if lit_content.as_str().is_ascii()
&& lit_content.as_str().len() <= MAX_LENGTH_BYTE_STRING_LIT
- && !args[0].span.from_expansion()
+ && !cx.tcx.hir().span(args[0].hir_id).from_expansion()
{
span_lint_and_sugg(
cx,
STRING_LIT_AS_BYTES,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
"calling `as_bytes()` on a string literal",
"consider using a byte string literal instead",
format!(
"b{}",
- snippet_with_applicability(cx, args[0].span, r#""foo""#, &mut applicability)
+ snippet_with_applicability(cx, cx.tcx.hir().span(args[0].hir_id), r#""foo""#, &mut applicability)
),
applicability,
);
@@ -329,7 +329,7 @@ impl LateLintPass<'_> for StrToString {
span_lint_and_help(
cx,
STR_TO_STRING,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"`to_string()` called on a `&str`",
None,
"consider using `.to_owned()`",
@@ -377,7 +377,7 @@ impl LateLintPass<'_> for StringToString {
span_lint_and_help(
cx,
STRING_TO_STRING,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"`to_string()` called on a `String`",
None,
"consider using `.clone()`",
diff --git a/src/tools/clippy/clippy_lints/src/swap.rs b/src/tools/clippy/clippy_lints/src/swap.rs
index 9d8a0c248334f..181179fd05a1f 100644
--- a/src/tools/clippy/clippy_lints/src/swap.rs
+++ b/src/tools/clippy/clippy_lints/src/swap.rs
@@ -116,8 +116,8 @@ fn check_manual_swap(cx: &LateContext<'_>, block: &Block<'_>) {
format!(
"{}.swap({}, {})",
slice.maybe_par(),
- snippet_with_applicability(cx, idx1.span, "..", &mut applicability),
- snippet_with_applicability(cx, idx2.span, "..", &mut applicability),
+ snippet_with_applicability(cx, cx.tcx.hir().span(idx1.hir_id), "..", &mut applicability),
+ snippet_with_applicability(cx, cx.tcx.hir().span(idx2.hir_id), "..", &mut applicability),
),
)
} else {
@@ -133,7 +133,7 @@ fn check_manual_swap(cx: &LateContext<'_>, block: &Block<'_>) {
(true, String::new(), String::new())
};
- let span = w[0].span.to(second.span);
+ let span = cx.tcx.hir().span(w[0].hir_id).to(cx.tcx.hir().span(second.hir_id));
span_lint_and_then(
cx,
@@ -218,7 +218,7 @@ fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) {
if_chain! {
if let StmtKind::Semi(ref first) = w[0].kind;
if let StmtKind::Semi(ref second) = w[1].kind;
- if !differing_macro_contexts(first.span, second.span);
+ if !differing_macro_contexts(cx.tcx.hir().span(first.hir_id), cx.tcx.hir().span(second.hir_id));
if let ExprKind::Assign(ref lhs0, ref rhs0, _) = first.kind;
if let ExprKind::Assign(ref lhs1, ref rhs1, _) = second.kind;
if eq_expr_value(cx, lhs0, rhs1);
@@ -236,7 +236,7 @@ fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) {
(String::new(), String::new(), String::new())
};
- let span = first.span.to(second.span);
+ let span = cx.tcx.hir().span(first.hir_id).to(cx.tcx.hir().span(second.hir_id));
span_lint_and_then(cx,
ALMOST_SWAPPED,
diff --git a/src/tools/clippy/clippy_lints/src/temporary_assignment.rs b/src/tools/clippy/clippy_lints/src/temporary_assignment.rs
index fb891866364cc..7ac88852d1f94 100644
--- a/src/tools/clippy/clippy_lints/src/temporary_assignment.rs
+++ b/src/tools/clippy/clippy_lints/src/temporary_assignment.rs
@@ -35,7 +35,7 @@ impl<'tcx> LateLintPass<'tcx> for TemporaryAssignment {
base = f;
}
if is_temporary(base) && !is_adjusted(cx, base) {
- span_lint(cx, TEMPORARY_ASSIGNMENT, expr.span, "assignment to temporary");
+ span_lint(cx, TEMPORARY_ASSIGNMENT, cx.tcx.hir().span(expr.hir_id), "assignment to temporary");
}
}
}
diff --git a/src/tools/clippy/clippy_lints/src/to_digit_is_some.rs b/src/tools/clippy/clippy_lints/src/to_digit_is_some.rs
index eeda39bfa2087..b22c6363a0c5e 100644
--- a/src/tools/clippy/clippy_lints/src/to_digit_is_some.rs
+++ b/src/tools/clippy/clippy_lints/src/to_digit_is_some.rs
@@ -71,13 +71,13 @@ impl<'tcx> LateLintPass<'tcx> for ToDigitIsSome {
if let Some((is_method_call, char_arg, radix_arg)) = match_result {
let mut applicability = Applicability::MachineApplicable;
- let char_arg_snip = snippet_with_applicability(cx, char_arg.span, "_", &mut applicability);
- let radix_snip = snippet_with_applicability(cx, radix_arg.span, "_", &mut applicability);
+ let char_arg_snip = snippet_with_applicability(cx, cx.tcx.hir().span(char_arg.hir_id), "_", &mut applicability);
+ let radix_snip = snippet_with_applicability(cx, cx.tcx.hir().span(radix_arg.hir_id), "_", &mut applicability);
span_lint_and_sugg(
cx,
TO_DIGIT_IS_SOME,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"use of `.to_digit(..).is_some()`",
"try this",
if is_method_call {
diff --git a/src/tools/clippy/clippy_lints/src/to_string_in_display.rs b/src/tools/clippy/clippy_lints/src/to_string_in_display.rs
index 84ec2aa18abcc..5948c25a88c15 100644
--- a/src/tools/clippy/clippy_lints/src/to_string_in_display.rs
+++ b/src/tools/clippy/clippy_lints/src/to_string_in_display.rs
@@ -100,7 +100,7 @@ impl LateLintPass<'_> for ToStringInDisplay {
span_lint(
cx,
TO_STRING_IN_DISPLAY,
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
"using `to_string` in `fmt::Display` implementation might lead to infinite recursion",
);
}
diff --git a/src/tools/clippy/clippy_lints/src/trait_bounds.rs b/src/tools/clippy/clippy_lints/src/trait_bounds.rs
index daff5f81e8c34..c3a5442cdef1b 100644
--- a/src/tools/clippy/clippy_lints/src/trait_bounds.rs
+++ b/src/tools/clippy/clippy_lints/src/trait_bounds.rs
@@ -111,7 +111,7 @@ impl TraitBounds {
then {
let mut hint_string = format!(
"consider combining the bounds: `{}:",
- snippet(cx, p.bounded_ty.span, "_")
+ snippet(cx, cx.tcx.hir().span(p.bounded_ty.hir_id), "_")
);
for b in v.iter() {
if let GenericBound::Trait(ref poly_trait_ref, _) = b {
diff --git a/src/tools/clippy/clippy_lints/src/transmute/crosspointer_transmute.rs b/src/tools/clippy/clippy_lints/src/transmute/crosspointer_transmute.rs
index ce87defaa9406..41010478cbde9 100644
--- a/src/tools/clippy/clippy_lints/src/transmute/crosspointer_transmute.rs
+++ b/src/tools/clippy/clippy_lints/src/transmute/crosspointer_transmute.rs
@@ -12,7 +12,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty
span_lint(
cx,
CROSSPOINTER_TRANSMUTE,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
&format!(
"transmute from a type (`{}`) to the type that it points to (`{}`)",
from_ty, to_ty
@@ -24,7 +24,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty
span_lint(
cx,
CROSSPOINTER_TRANSMUTE,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
&format!(
"transmute from a type (`{}`) to a pointer to that type (`{}`)",
from_ty, to_ty
diff --git a/src/tools/clippy/clippy_lints/src/transmute/transmute_float_to_int.rs b/src/tools/clippy/clippy_lints/src/transmute/transmute_float_to_int.rs
index 562d880e39afb..8a132cea155d1 100644
--- a/src/tools/clippy/clippy_lints/src/transmute/transmute_float_to_int.rs
+++ b/src/tools/clippy/clippy_lints/src/transmute/transmute_float_to_int.rs
@@ -22,7 +22,7 @@ pub(super) fn check<'tcx>(
span_lint_and_then(
cx,
TRANSMUTE_FLOAT_TO_INT,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
|diag| {
let mut expr = &args[0];
@@ -55,7 +55,12 @@ pub(super) fn check<'tcx>(
arg
};
- diag.span_suggestion(e.span, "consider using", arg.to_string(), Applicability::Unspecified);
+ diag.span_suggestion(
+ cx.tcx.hir().span(e.hir_id),
+ "consider using",
+ arg.to_string(),
+ Applicability::Unspecified,
+ );
},
);
true
diff --git a/src/tools/clippy/clippy_lints/src/transmute/transmute_int_to_bool.rs b/src/tools/clippy/clippy_lints/src/transmute/transmute_int_to_bool.rs
index 5b609f906a3d7..f1302ad1642cc 100644
--- a/src/tools/clippy/clippy_lints/src/transmute/transmute_int_to_bool.rs
+++ b/src/tools/clippy/clippy_lints/src/transmute/transmute_int_to_bool.rs
@@ -21,13 +21,13 @@ pub(super) fn check<'tcx>(
span_lint_and_then(
cx,
TRANSMUTE_INT_TO_BOOL,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
&format!("transmute from a `{}` to a `bool`", from_ty),
|diag| {
let arg = sugg::Sugg::hir(cx, &args[0], "..");
let zero = sugg::Sugg::NonParen(Cow::from("0"));
diag.span_suggestion(
- e.span,
+ cx.tcx.hir().span(e.hir_id),
"consider using",
sugg::make_binop(ast::BinOpKind::Ne, &arg, &zero).to_string(),
Applicability::Unspecified,
diff --git a/src/tools/clippy/clippy_lints/src/transmute/transmute_int_to_char.rs b/src/tools/clippy/clippy_lints/src/transmute/transmute_int_to_char.rs
index 29d2450618a7e..7291bc6a7005c 100644
--- a/src/tools/clippy/clippy_lints/src/transmute/transmute_int_to_char.rs
+++ b/src/tools/clippy/clippy_lints/src/transmute/transmute_int_to_char.rs
@@ -20,7 +20,7 @@ pub(super) fn check<'tcx>(
span_lint_and_then(
cx,
TRANSMUTE_INT_TO_CHAR,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
&format!("transmute from a `{}` to a `char`", from_ty),
|diag| {
let arg = sugg::Sugg::hir(cx, &args[0], "..");
@@ -30,7 +30,7 @@ pub(super) fn check<'tcx>(
arg
};
diag.span_suggestion(
- e.span,
+ cx.tcx.hir().span(e.hir_id),
"consider using",
format!("std::char::from_u32({}).unwrap()", arg.to_string()),
Applicability::Unspecified,
diff --git a/src/tools/clippy/clippy_lints/src/transmute/transmute_int_to_float.rs b/src/tools/clippy/clippy_lints/src/transmute/transmute_int_to_float.rs
index f83fba8966a11..13f2d9350abbd 100644
--- a/src/tools/clippy/clippy_lints/src/transmute/transmute_int_to_float.rs
+++ b/src/tools/clippy/clippy_lints/src/transmute/transmute_int_to_float.rs
@@ -20,7 +20,7 @@ pub(super) fn check<'tcx>(
span_lint_and_then(
cx,
TRANSMUTE_INT_TO_FLOAT,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
|diag| {
let arg = sugg::Sugg::hir(cx, &args[0], "..");
@@ -33,7 +33,7 @@ pub(super) fn check<'tcx>(
arg
};
diag.span_suggestion(
- e.span,
+ cx.tcx.hir().span(e.hir_id),
"consider using",
format!("{}::from_bits({})", to_ty, arg.to_string()),
Applicability::Unspecified,
diff --git a/src/tools/clippy/clippy_lints/src/transmute/transmute_ptr_to_ptr.rs b/src/tools/clippy/clippy_lints/src/transmute/transmute_ptr_to_ptr.rs
index f4e60a3020cf5..19b6d26c38439 100644
--- a/src/tools/clippy/clippy_lints/src/transmute/transmute_ptr_to_ptr.rs
+++ b/src/tools/clippy/clippy_lints/src/transmute/transmute_ptr_to_ptr.rs
@@ -19,12 +19,17 @@ pub(super) fn check<'tcx>(
span_lint_and_then(
cx,
TRANSMUTE_PTR_TO_PTR,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
"transmute from a pointer to a pointer",
|diag| {
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
let sugg = arg.as_ty(cx.tcx.mk_ptr(*to_ty));
- diag.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
+ diag.span_suggestion(
+ cx.tcx.hir().span(e.hir_id),
+ "try",
+ sugg.to_string(),
+ Applicability::Unspecified,
+ );
}
},
);
diff --git a/src/tools/clippy/clippy_lints/src/transmute/transmute_ptr_to_ref.rs b/src/tools/clippy/clippy_lints/src/transmute/transmute_ptr_to_ref.rs
index f5dbbbe33bc64..2d209e9d74430 100644
--- a/src/tools/clippy/clippy_lints/src/transmute/transmute_ptr_to_ref.rs
+++ b/src/tools/clippy/clippy_lints/src/transmute/transmute_ptr_to_ref.rs
@@ -21,7 +21,7 @@ pub(super) fn check<'tcx>(
span_lint_and_then(
cx,
TRANSMUTE_PTR_TO_REF,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
&format!(
"transmute from a pointer type (`{}`) to a reference type (`{}`)",
from_ty, to_ty
@@ -41,7 +41,7 @@ pub(super) fn check<'tcx>(
};
diag.span_suggestion(
- e.span,
+ cx.tcx.hir().span(e.hir_id),
"try",
sugg::make_unop(deref, arg).to_string(),
Applicability::Unspecified,
diff --git a/src/tools/clippy/clippy_lints/src/transmute/transmute_ref_to_ref.rs b/src/tools/clippy/clippy_lints/src/transmute/transmute_ref_to_ref.rs
index 01b00bb0a2229..d12822fdc9f12 100644
--- a/src/tools/clippy/clippy_lints/src/transmute/transmute_ref_to_ref.rs
+++ b/src/tools/clippy/clippy_lints/src/transmute/transmute_ref_to_ref.rs
@@ -33,13 +33,13 @@ pub(super) fn check<'tcx>(
span_lint_and_sugg(
cx,
TRANSMUTE_BYTES_TO_STR,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
"consider using",
format!(
"std::str::from_utf8{}({}).unwrap()",
postfix,
- snippet(cx, args[0].span, ".."),
+ snippet(cx, cx.tcx.hir().span(args[0].hir_id), ".."),
),
Applicability::Unspecified,
);
@@ -50,7 +50,7 @@ pub(super) fn check<'tcx>(
span_lint_and_then(
cx,
TRANSMUTE_PTR_TO_PTR,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
"transmute from a reference to a reference",
|diag| if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
let ty_from_and_mut = ty::TypeAndMut {
@@ -67,7 +67,7 @@ pub(super) fn check<'tcx>(
sugg_paren.addr_deref()
};
diag.span_suggestion(
- e.span,
+ cx.tcx.hir().span(e.hir_id),
"try",
sugg.to_string(),
Applicability::Unspecified,
diff --git a/src/tools/clippy/clippy_lints/src/transmute/transmutes_expressible_as_ptr_casts.rs b/src/tools/clippy/clippy_lints/src/transmute/transmutes_expressible_as_ptr_casts.rs
index dea896622f11c..e12b41bb44822 100644
--- a/src/tools/clippy/clippy_lints/src/transmute/transmutes_expressible_as_ptr_casts.rs
+++ b/src/tools/clippy/clippy_lints/src/transmute/transmutes_expressible_as_ptr_casts.rs
@@ -19,7 +19,7 @@ pub(super) fn check<'tcx>(
span_lint_and_then(
cx,
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
&format!(
"transmute from `{}` to `{}` which could be expressed as a pointer cast instead",
from_ty, to_ty
@@ -27,7 +27,12 @@ pub(super) fn check<'tcx>(
|diag| {
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
let sugg = arg.as_ty(&to_ty.to_string()).to_string();
- diag.span_suggestion(e.span, "try", sugg, Applicability::MachineApplicable);
+ diag.span_suggestion(
+ cx.tcx.hir().span(e.hir_id),
+ "try",
+ sugg,
+ Applicability::MachineApplicable,
+ );
}
},
);
diff --git a/src/tools/clippy/clippy_lints/src/transmute/unsound_collection_transmute.rs b/src/tools/clippy/clippy_lints/src/transmute/unsound_collection_transmute.rs
index 503c5e0ff3822..0f93085c5c58e 100644
--- a/src/tools/clippy/clippy_lints/src/transmute/unsound_collection_transmute.rs
+++ b/src/tools/clippy/clippy_lints/src/transmute/unsound_collection_transmute.rs
@@ -32,7 +32,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty
span_lint(
cx,
UNSOUND_COLLECTION_TRANSMUTE,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
&format!(
"transmute from `{}` to `{}` with mismatched layout is unsound",
from_ty, to_ty
diff --git a/src/tools/clippy/clippy_lints/src/transmute/useless_transmute.rs b/src/tools/clippy/clippy_lints/src/transmute/useless_transmute.rs
index 83441514af051..5931f48692b2e 100644
--- a/src/tools/clippy/clippy_lints/src/transmute/useless_transmute.rs
+++ b/src/tools/clippy/clippy_lints/src/transmute/useless_transmute.rs
@@ -19,7 +19,7 @@ pub(super) fn check<'tcx>(
span_lint(
cx,
USELESS_TRANSMUTE,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
&format!("transmute from a type (`{}`) to itself", from_ty),
);
true
@@ -28,7 +28,7 @@ pub(super) fn check<'tcx>(
span_lint_and_then(
cx,
USELESS_TRANSMUTE,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
"transmute from a reference to a pointer",
|diag| {
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
@@ -43,7 +43,12 @@ pub(super) fn check<'tcx>(
arg.as_ty(cx.tcx.mk_ptr(rty_and_mut)).as_ty(to_ty)
};
- diag.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
+ diag.span_suggestion(
+ cx.tcx.hir().span(e.hir_id),
+ "try",
+ sugg.to_string(),
+ Applicability::Unspecified,
+ );
}
},
);
@@ -53,12 +58,12 @@ pub(super) fn check<'tcx>(
span_lint_and_then(
cx,
USELESS_TRANSMUTE,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
"transmute from an integer to a pointer",
|diag| {
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
diag.span_suggestion(
- e.span,
+ cx.tcx.hir().span(e.hir_id),
"try",
arg.as_ty(&to_ty.to_string()).to_string(),
Applicability::Unspecified,
diff --git a/src/tools/clippy/clippy_lints/src/transmute/utils.rs b/src/tools/clippy/clippy_lints/src/transmute/utils.rs
index 55008d8ec3f16..f5538a724536f 100644
--- a/src/tools/clippy/clippy_lints/src/transmute/utils.rs
+++ b/src/tools/clippy/clippy_lints/src/transmute/utils.rs
@@ -22,7 +22,7 @@ pub(super) fn get_type_snippet(cx: &LateContext<'_>, path: &QPath<'_>, to_ref_ty
}).nth(1);
if let TyKind::Rptr(_, ref to_ty) = to_ty.kind;
then {
- return snippet(cx, to_ty.ty.span, &to_ref_ty.to_string()).to_string();
+ return snippet(cx, cx.tcx.hir().span(to_ty.ty.hir_id), &to_ref_ty.to_string()).to_string();
}
}
diff --git a/src/tools/clippy/clippy_lints/src/transmute/wrong_transmute.rs b/src/tools/clippy/clippy_lints/src/transmute/wrong_transmute.rs
index d6d77f2c83456..788d7bcb821d7 100644
--- a/src/tools/clippy/clippy_lints/src/transmute/wrong_transmute.rs
+++ b/src/tools/clippy/clippy_lints/src/transmute/wrong_transmute.rs
@@ -12,7 +12,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty
span_lint(
cx,
WRONG_TRANSMUTE,
- e.span,
+ cx.tcx.hir().span(e.hir_id),
&format!("transmute from a `{}` to a pointer", from_ty),
);
true
diff --git a/src/tools/clippy/clippy_lints/src/transmuting_null.rs b/src/tools/clippy/clippy_lints/src/transmuting_null.rs
index 2ba2b646f004f..df1e439f578dc 100644
--- a/src/tools/clippy/clippy_lints/src/transmuting_null.rs
+++ b/src/tools/clippy/clippy_lints/src/transmuting_null.rs
@@ -31,7 +31,7 @@ const LINT_MSG: &str = "transmuting a known null pointer into a reference";
impl<'tcx> LateLintPass<'tcx> for TransmutingNull {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
- if in_external_macro(cx.sess(), expr.span) {
+ if in_external_macro(cx.sess(), cx.tcx.hir().span(expr.hir_id)) {
return;
}
@@ -50,7 +50,7 @@ impl<'tcx> LateLintPass<'tcx> for TransmutingNull {
let x = const_eval_context.expr(&args[0]);
if let Some(Constant::RawPtr(0)) = x;
then {
- span_lint(cx, TRANSMUTING_NULL, expr.span, LINT_MSG)
+ span_lint(cx, TRANSMUTING_NULL, cx.tcx.hir().span(expr.hir_id), LINT_MSG)
}
}
@@ -61,7 +61,7 @@ impl<'tcx> LateLintPass<'tcx> for TransmutingNull {
if let ExprKind::Lit(ref lit) = inner_expr.kind;
if let LitKind::Int(0, _) = lit.node;
then {
- span_lint(cx, TRANSMUTING_NULL, expr.span, LINT_MSG)
+ span_lint(cx, TRANSMUTING_NULL, cx.tcx.hir().span(expr.hir_id), LINT_MSG)
}
}
@@ -73,7 +73,7 @@ impl<'tcx> LateLintPass<'tcx> for TransmutingNull {
if match_qpath(path1, &paths::STD_PTR_NULL);
if args1.is_empty();
then {
- span_lint(cx, TRANSMUTING_NULL, expr.span, LINT_MSG)
+ span_lint(cx, TRANSMUTING_NULL, cx.tcx.hir().span(expr.hir_id), LINT_MSG)
}
}
diff --git a/src/tools/clippy/clippy_lints/src/try_err.rs b/src/tools/clippy/clippy_lints/src/try_err.rs
index 73e3a04aec987..91506ca11a193 100644
--- a/src/tools/clippy/clippy_lints/src/try_err.rs
+++ b/src/tools/clippy/clippy_lints/src/try_err.rs
@@ -59,7 +59,7 @@ impl<'tcx> LateLintPass<'tcx> for TryErr {
// val,
// };
if_chain! {
- if !in_external_macro(cx.tcx.sess, expr.span);
+ if !in_external_macro(cx.tcx.sess, cx.tcx.hir().span(expr.hir_id));
if let ExprKind::Match(ref match_arg, _, MatchSource::TryDesugar) = expr.kind;
if let ExprKind::Call(ref match_fun, ref try_args) = match_arg.kind;
if let ExprKind::Path(ref match_fun_path) = match_fun.kind;
@@ -92,14 +92,16 @@ impl<'tcx> LateLintPass<'tcx> for TryErr {
};
let expr_err_ty = cx.typeck_results().expr_ty(err_arg);
- let differing_contexts = differing_macro_contexts(expr.span, err_arg.span);
-
- let origin_snippet = if in_macro(expr.span) && in_macro(err_arg.span) && differing_contexts {
- snippet(cx, err_arg.span.ctxt().outer_expn_data().call_site, "_")
- } else if err_arg.span.from_expansion() && !in_macro(expr.span) {
- snippet_with_macro_callsite(cx, err_arg.span, "_")
+ let expr_span = cx.tcx.hir().span(expr.hir_id);
+ let err_arg_span = cx.tcx.hir().span(err_arg.hir_id);
+ let differing_contexts = differing_macro_contexts(expr_span, err_arg_span);
+
+ let origin_snippet = if in_macro(expr_span) && in_macro(err_arg_span) && differing_contexts {
+ snippet(cx, err_arg_span.ctxt().outer_expn_data().call_site, "_")
+ } else if err_arg_span.from_expansion() && !in_macro(expr_span) {
+ snippet_with_macro_callsite(cx, err_arg_span, "_")
} else {
- snippet(cx, err_arg.span, "_")
+ snippet(cx, err_arg_span, "_")
};
let suggestion = if err_ty == expr_err_ty {
format!("return {}{}{}", prefix, origin_snippet, suffix)
@@ -110,7 +112,7 @@ impl<'tcx> LateLintPass<'tcx> for TryErr {
span_lint_and_sugg(
cx,
TRY_ERR,
- expr.span,
+ expr_span,
"returning an `Err(_)` with the `?` operator",
"try this",
suggestion,
diff --git a/src/tools/clippy/clippy_lints/src/types/borrowed_box.rs b/src/tools/clippy/clippy_lints/src/types/borrowed_box.rs
index a7a511b21cf59..46dc8eddaab08 100644
--- a/src/tools/clippy/clippy_lints/src/types/borrowed_box.rs
+++ b/src/tools/clippy/clippy_lints/src/types/borrowed_box.rs
@@ -48,7 +48,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, lt: &Lifetime, m
// When trait objects or opaque types have lifetime or auto-trait bounds,
// we need to add parentheses to avoid a syntax error due to its ambiguity.
// Originally reported as the issue #3128.
- let inner_snippet = snippet(cx, inner.span, "..");
+ let inner_snippet = snippet(cx, cx.tcx.hir().span(inner.hir_id), "..");
let suggestion = match &inner.kind {
TyKind::TraitObject(bounds, lt_bound) if bounds.len() > 1 || !lt_bound.is_elided() => {
format!("&{}({})", ltopt, &inner_snippet)
@@ -64,7 +64,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, lt: &Lifetime, m
span_lint_and_sugg(
cx,
BORROWED_BOX,
- hir_ty.span,
+ cx.tcx.hir().span(hir_ty.hir_id),
"you seem to be trying to use `&Box`. Consider using just `&T`",
"try",
suggestion,
diff --git a/src/tools/clippy/clippy_lints/src/types/box_vec.rs b/src/tools/clippy/clippy_lints/src/types/box_vec.rs
index 6aa98e435e160..a87a74274e5af 100644
--- a/src/tools/clippy/clippy_lints/src/types/box_vec.rs
+++ b/src/tools/clippy/clippy_lints/src/types/box_vec.rs
@@ -13,7 +13,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
span_lint_and_help(
cx,
BOX_VEC,
- hir_ty.span,
+ cx.tcx.hir().span(hir_ty.hir_id),
"you seem to be trying to use `Box>`. Consider using just `Vec`",
None,
"`Vec` is already on the heap, `Box>` makes an extra allocation",
diff --git a/src/tools/clippy/clippy_lints/src/types/linked_list.rs b/src/tools/clippy/clippy_lints/src/types/linked_list.rs
index 47eb4ede4e422..d2ff79f0ab7ff 100644
--- a/src/tools/clippy/clippy_lints/src/types/linked_list.rs
+++ b/src/tools/clippy/clippy_lints/src/types/linked_list.rs
@@ -10,7 +10,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, def_id: DefId) -
span_lint_and_help(
cx,
LINKEDLIST,
- hir_ty.span,
+ cx.tcx.hir().span(hir_ty.hir_id),
"you seem to be using a `LinkedList`! Perhaps you meant some other data structure?",
None,
"a `VecDeque` might work",
diff --git a/src/tools/clippy/clippy_lints/src/types/mod.rs b/src/tools/clippy/clippy_lints/src/types/mod.rs
index 13da768b0ca3e..79354d3ffb6f6 100644
--- a/src/tools/clippy/clippy_lints/src/types/mod.rs
+++ b/src/tools/clippy/clippy_lints/src/types/mod.rs
@@ -260,7 +260,7 @@ pub struct Types {
impl_lint_pass!(Types => [BOX_VEC, VEC_BOX, OPTION_OPTION, LINKEDLIST, BORROWED_BOX, REDUNDANT_ALLOCATION, RC_BUFFER]);
impl<'tcx> LateLintPass<'tcx> for Types {
- fn check_fn(&mut self, cx: &LateContext<'_>, _: FnKind<'_>, decl: &FnDecl<'_>, _: &Body<'_>, _: Span, id: HirId) {
+ fn check_fn(&mut self, cx: &LateContext<'_>, _: FnKind<'_>, decl: &FnDecl<'_>, _: &Body<'_>, id: HirId) {
// Skip trait implementations; see issue #605.
if let Some(hir::Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_item(id)) {
if let ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = item.kind {
@@ -310,7 +310,8 @@ impl Types {
///
/// The parameter `is_local` distinguishes the context of the type.
fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, is_local: bool) {
- if hir_ty.span.from_expansion() {
+ let hir_ty_span = cx.tcx.hir().span(hir_ty.hir_id);
+ if hir_ty_span.from_expansion() {
return;
}
match hir_ty.kind {
@@ -415,7 +416,8 @@ impl<'tcx> LateLintPass<'tcx> for LetUnitValue {
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
if let StmtKind::Local(ref local) = stmt.kind {
if is_unit(cx.typeck_results().pat_ty(&local.pat)) {
- if in_external_macro(cx.sess(), stmt.span) || local.pat.span.from_expansion() {
+ let stmt_span = cx.tcx.hir().span(stmt.hir_id);
+ if in_external_macro(cx.sess(), stmt_span) || cx.tcx.hir().span(local.pat.hir_id).from_expansion() {
return;
}
if higher::is_from_for_desugar(local) {
@@ -424,13 +426,13 @@ impl<'tcx> LateLintPass<'tcx> for LetUnitValue {
span_lint_and_then(
cx,
LET_UNIT_VALUE,
- stmt.span,
+ stmt_span,
"this let-binding has unit value",
|diag| {
if let Some(expr) = &local.init {
- let snip = snippet_with_macro_callsite(cx, expr.span, "()");
+ let snip = snippet_with_macro_callsite(cx, cx.tcx.hir().span(expr.hir_id), "()");
diag.span_suggestion(
- stmt.span,
+ stmt_span,
"omit the `let` binding",
format!("{};", snip),
Applicability::MachineApplicable, // snippet
@@ -494,8 +496,9 @@ declare_lint_pass!(UnitCmp => [UNIT_CMP]);
impl<'tcx> LateLintPass<'tcx> for UnitCmp {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
- if expr.span.from_expansion() {
- if let Some(callee) = expr.span.source_callee() {
+ let expr_span = cx.tcx.hir().span(expr.hir_id);
+ if expr_span.from_expansion() {
+ if let Some(callee) = expr_span.source_callee() {
if let ExpnKind::Macro(MacroKind::Bang, symbol) = callee.kind {
if let ExprKind::Binary(ref cmp, ref left, _) = expr.kind {
let op = cmp.node;
@@ -508,7 +511,7 @@ impl<'tcx> LateLintPass<'tcx> for UnitCmp {
span_lint(
cx,
UNIT_CMP,
- expr.span,
+ expr_span,
&format!(
"`{}` of unit values detected. This will always {}",
symbol.as_str(),
@@ -531,7 +534,7 @@ impl<'tcx> LateLintPass<'tcx> for UnitCmp {
span_lint(
cx,
UNIT_CMP,
- expr.span,
+ expr_span,
&format!(
"{}-comparison of unit values detected. This will always be {}",
op.as_str(),
@@ -567,7 +570,7 @@ declare_lint_pass!(UnitArg => [UNIT_ARG]);
impl<'tcx> LateLintPass<'tcx> for UnitArg {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
- if expr.span.from_expansion() {
+ if cx.tcx.hir().span(expr.hir_id).from_expansion() {
return;
}
@@ -575,14 +578,14 @@ impl<'tcx> LateLintPass<'tcx> for UnitArg {
// so check for that here
// only the calls to `Try::from_error` is marked as desugared,
// so we need to check both the current Expr and its parent.
- if is_questionmark_desugar_marked_call(expr) {
+ if is_questionmark_desugar_marked_call(cx, expr) {
return;
}
if_chain! {
let map = &cx.tcx.hir();
let opt_parent_node = map.find(map.get_parent_node(expr.hir_id));
if let Some(hir::Node::Expr(parent_expr)) = opt_parent_node;
- if is_questionmark_desugar_marked_call(parent_expr);
+ if is_questionmark_desugar_marked_call(cx, parent_expr);
then {
return;
}
@@ -619,7 +622,7 @@ fn fmt_stmts_and_call(
args_snippets: &[impl AsRef],
non_empty_block_args_snippets: &[impl AsRef],
) -> String {
- let call_expr_indent = indent_of(cx, call_expr.span).unwrap_or(0);
+ let call_expr_indent = indent_of(cx, cx.tcx.hir().span(call_expr.hir_id)).unwrap_or(0);
let call_snippet_with_replacements = args_snippets
.iter()
.fold(call_snippet.to_owned(), |acc, arg| acc.replacen(arg.as_ref(), "()", 1));
@@ -658,10 +661,11 @@ fn lint_unit_args(cx: &LateContext<'_>, expr: &Expr<'_>, args_to_recover: &[&Exp
} else {
("a ", "")
};
+ let expr_span = cx.tcx.hir().span(expr.hir_id);
span_lint_and_then(
cx,
UNIT_ARG,
- expr.span,
+ expr_span,
&format!("passing {}unit value{} to a function", singular, plural),
|db| {
let mut or = "";
@@ -673,10 +677,10 @@ fn lint_unit_args(cx: &LateContext<'_>, expr: &Expr<'_>, args_to_recover: &[&Exp
if block.expr.is_none();
if let Some(last_stmt) = block.stmts.iter().last();
if let StmtKind::Semi(last_expr) = last_stmt.kind;
- if let Some(snip) = snippet_opt(cx, last_expr.span);
+ if let Some(snip) = snippet_opt(cx, cx.tcx.hir().span(last_expr.hir_id));
then {
Some((
- last_stmt.span,
+ cx.tcx.hir().span(last_stmt.hir_id),
snip,
))
}
@@ -698,15 +702,15 @@ fn lint_unit_args(cx: &LateContext<'_>, expr: &Expr<'_>, args_to_recover: &[&Exp
let arg_snippets: Vec = args_to_recover
.iter()
- .filter_map(|arg| snippet_opt(cx, arg.span))
+ .filter_map(|arg| snippet_opt(cx, cx.tcx.hir().span(arg.hir_id)))
.collect();
let arg_snippets_without_empty_blocks: Vec = args_to_recover
.iter()
.filter(|arg| !is_empty_block(arg))
- .filter_map(|arg| snippet_opt(cx, arg.span))
+ .filter_map(|arg| snippet_opt(cx, cx.tcx.hir().span(arg.hir_id)))
.collect();
- if let Some(call_snippet) = snippet_opt(cx, expr.span) {
+ if let Some(call_snippet) = snippet_opt(cx, cx.tcx.hir().span(expr.hir_id)) {
let sugg = fmt_stmts_and_call(
cx,
expr,
@@ -720,7 +724,7 @@ fn lint_unit_args(cx: &LateContext<'_>, expr: &Expr<'_>, args_to_recover: &[&Exp
&format!("use {}unit literal{} instead", singular, plural),
args_to_recover
.iter()
- .map(|arg| (arg.span, "()".to_string()))
+ .map(|arg| (cx.tcx.hir().span(arg.hir_id), "()".to_string()))
.collect::>(),
applicability,
);
@@ -729,7 +733,7 @@ fn lint_unit_args(cx: &LateContext<'_>, expr: &Expr<'_>, args_to_recover: &[&Exp
let empty_or_s = if plural { "s" } else { "" };
let it_or_them = if plural { "them" } else { "it" };
db.span_suggestion(
- expr.span,
+ cx.tcx.hir().span(expr.hir_id),
&format!(
"{}move the expression{} in front of the call and replace {} with the unit literal `()`",
or, empty_or_s, it_or_them
@@ -757,10 +761,13 @@ fn is_empty_block(expr: &Expr<'_>) -> bool {
)
}
-fn is_questionmark_desugar_marked_call(expr: &Expr<'_>) -> bool {
+fn is_questionmark_desugar_marked_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
use rustc_span::hygiene::DesugaringKind;
if let ExprKind::Call(ref callee, _) = expr.kind {
- callee.span.is_desugaring(DesugaringKind::QuestionMark)
+ cx.tcx
+ .hir()
+ .span(callee.hir_id)
+ .is_desugaring(DesugaringKind::QuestionMark)
} else {
false
}
@@ -815,7 +822,6 @@ impl<'tcx> LateLintPass<'tcx> for TypeComplexity {
_: FnKind<'tcx>,
decl: &'tcx FnDecl<'_>,
_: &'tcx Body<'_>,
- _: Span,
_: HirId,
) {
self.check_fndecl(cx, decl);
@@ -869,7 +875,7 @@ impl<'tcx> TypeComplexity {
}
fn check_type(&self, cx: &LateContext<'_>, ty: &hir::Ty<'_>) {
- if ty.span.from_expansion() {
+ if cx.tcx.hir().span(ty.hir_id).from_expansion() {
return;
}
let score = {
@@ -882,7 +888,7 @@ impl<'tcx> TypeComplexity {
span_lint(
cx,
TYPE_COMPLEXITY,
- ty.span,
+ cx.tcx.hir().span(ty.hir_id),
"very complex type used. Consider factoring parts into `type` definitions",
);
}
@@ -1075,7 +1081,7 @@ impl<'tcx> LateLintPass<'tcx> for AbsurdExtremeComparisons {
if let ExprKind::Binary(ref cmp, ref lhs, ref rhs) = expr.kind {
if let Some((culprit, result)) = detect_absurd_comparison(cx, cmp.node, lhs, rhs) {
- if !expr.span.from_expansion() {
+ if !cx.tcx.hir().span(expr.hir_id).from_expansion() {
let msg = "this comparison involving the minimum or maximum element for this \
type contains a case that is always true or always false";
@@ -1085,14 +1091,14 @@ impl<'tcx> LateLintPass<'tcx> for AbsurdExtremeComparisons {
InequalityImpossible => format!(
"the case where the two sides are not equal never occurs, consider using `{} == {}` \
instead",
- snippet(cx, lhs.span, "lhs"),
- snippet(cx, rhs.span, "rhs")
+ snippet(cx, cx.tcx.hir().span(lhs.hir_id), "lhs"),
+ snippet(cx, cx.tcx.hir().span(rhs.hir_id), "rhs")
),
};
let help = format!(
"because `{}` is the {} value for this type, {}",
- snippet(cx, culprit.expr.span, "x"),
+ snippet(cx, cx.tcx.hir().span(culprit.expr.hir_id), "x"),
match culprit.which {
Minimum => "minimum",
Maximum => "maximum",
@@ -1100,7 +1106,14 @@ impl<'tcx> LateLintPass<'tcx> for AbsurdExtremeComparisons {
conclusion
);
- span_lint_and_help(cx, ABSURD_EXTREME_COMPARISONS, expr.span, msg, None, &help);
+ span_lint_and_help(
+ cx,
+ ABSURD_EXTREME_COMPARISONS,
+ cx.tcx.hir().span(expr.hir_id),
+ msg,
+ None,
+ &help,
+ );
}
}
}
@@ -1231,7 +1244,7 @@ fn err_upcast_comparison(cx: &LateContext<'_>, span: Span, expr: &Expr<'_>, alwa
span,
&format!(
"because of the numeric bounds on `{}` prior to casting, this expression is always {}",
- snippet(cx, cast_val.span, "the expression"),
+ snippet(cx, cx.tcx.hir().span(cast_val.hir_id), "the expression"),
if always { "true" } else { "false" },
),
);
@@ -1309,8 +1322,9 @@ impl<'tcx> LateLintPass<'tcx> for InvalidUpcastComparisons {
let lhs_bounds = numeric_cast_precast_bounds(cx, normalized_lhs);
let rhs_bounds = numeric_cast_precast_bounds(cx, normalized_rhs);
- upcast_comparison_bounds_err(cx, expr.span, rel, lhs_bounds, normalized_lhs, normalized_rhs, false);
- upcast_comparison_bounds_err(cx, expr.span, rel, rhs_bounds, normalized_rhs, normalized_lhs, true);
+ let expr_span = cx.tcx.hir().span(expr.hir_id);
+ upcast_comparison_bounds_err(cx, expr_span, rel, lhs_bounds, normalized_lhs, normalized_rhs, false);
+ upcast_comparison_bounds_err(cx, expr_span, rel, rhs_bounds, normalized_rhs, normalized_lhs, true);
}
}
}
@@ -1407,21 +1421,23 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitHasher {
return;
}
+ let item_span = cx.tcx.hir().span(item.hir_id());
+
match item.kind {
ItemKind::Impl(ref impl_) => {
let mut vis = ImplicitHasherTypeVisitor::new(cx);
vis.visit_ty(impl_.self_ty);
for target in &vis.found {
- if differing_macro_contexts(item.span, target.span()) {
+ if differing_macro_contexts(item_span, target.span()) {
return;
}
let generics_suggestion_span = impl_.generics.span.substitute_dummy({
- let pos = snippet_opt(cx, item.span.until(target.span()))
- .and_then(|snip| Some(item.span.lo() + BytePos(snip.find("impl")? as u32 + 4)));
+ let pos = snippet_opt(cx, item_span.until(target.span()))
+ .and_then(|snip| Some(item_span.lo() + BytePos(snip.find("impl")? as u32 + 4)));
if let Some(pos) = pos {
- Span::new(pos, pos, item.span.data().ctxt)
+ Span::new(pos, pos, item_span.data().ctxt)
} else {
return;
}
@@ -1458,13 +1474,13 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitHasher {
continue;
}
let generics_suggestion_span = generics.span.substitute_dummy({
- let pos = snippet_opt(cx, item.span.until(body.params[0].pat.span))
+ let pos = snippet_opt(cx, item_span.until(cx.tcx.hir().span(body.params[0].pat.hir_id)))
.and_then(|snip| {
let i = snip.find("fn")?;
- Some(item.span.lo() + BytePos((i + (&snip[i..]).find('(')?) as u32))
+ Some(item_span.lo() + BytePos((i + (&snip[i..]).find('(')?) as u32))
})
.expect("failed to create span for type parameters");
- Span::new(pos, pos, item.span.data().ctxt)
+ Span::new(pos, pos, item_span.data().ctxt)
});
let mut ctr_vis = ImplicitHasherConstructorVisitor::new(cx, target);
@@ -1518,16 +1534,16 @@ impl<'tcx> ImplicitHasherType<'tcx> {
if is_type_diagnostic_item(cx, ty, sym::hashmap_type) && params_len == 2 {
Some(ImplicitHasherType::HashMap(
- hir_ty.span,
+ cx.tcx.hir().span(hir_ty.hir_id),
ty,
- snippet(cx, params[0].span, "K"),
- snippet(cx, params[1].span, "V"),
+ snippet(cx, cx.tcx.hir().span(params[0].hir_id), "K"),
+ snippet(cx, cx.tcx.hir().span(params[1].hir_id), "V"),
))
} else if is_type_diagnostic_item(cx, ty, sym::hashset_type) && params_len == 1 {
Some(ImplicitHasherType::HashSet(
- hir_ty.span,
+ cx.tcx.hir().span(hir_ty.hir_id),
ty,
- snippet(cx, params[0].span, "T"),
+ snippet(cx, cx.tcx.hir().span(params[0].hir_id), "T"),
))
} else {
None
@@ -1629,29 +1645,30 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for ImplicitHasherConstructorVisitor<'a, 'b, 't
return;
}
+ let e_span = self.cx.tcx.hir().span(e.hir_id);
if match_path(ty_path, &paths::HASHMAP) {
if method.ident.name == sym::new {
self.suggestions
- .insert(e.span, "HashMap::default()".to_string());
+ .insert(e_span, "HashMap::default()".to_string());
} else if method.ident.name == sym!(with_capacity) {
self.suggestions.insert(
- e.span,
+ e_span,
format!(
"HashMap::with_capacity_and_hasher({}, Default::default())",
- snippet(self.cx, args[0].span, "capacity"),
+ snippet(self.cx, self.cx.tcx.hir().span(args[0].hir_id), "capacity"),
),
);
}
} else if match_path(ty_path, &paths::HASHSET) {
if method.ident.name == sym::new {
self.suggestions
- .insert(e.span, "HashSet::default()".to_string());
+ .insert(e_span, "HashSet::default()".to_string());
} else if method.ident.name == sym!(with_capacity) {
self.suggestions.insert(
- e.span,
+ e_span,
format!(
"HashSet::with_capacity_and_hasher({}, Default::default())",
- snippet(self.cx, args[0].span, "capacity"),
+ snippet(self.cx, self.cx.tcx.hir().span(args[0].hir_id), "capacity"),
),
);
}
diff --git a/src/tools/clippy/clippy_lints/src/types/option_option.rs b/src/tools/clippy/clippy_lints/src/types/option_option.rs
index dc5db963b4e98..21b0ed67b6bde 100644
--- a/src/tools/clippy/clippy_lints/src/types/option_option.rs
+++ b/src/tools/clippy/clippy_lints/src/types/option_option.rs
@@ -13,7 +13,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
span_lint(
cx,
OPTION_OPTION,
- hir_ty.span,
+ cx.tcx.hir().span(hir_ty.hir_id),
"consider using `Option` instead of `Option