Skip to content

Commit c057621

Browse files
committed
Auto merge of #6030 - rail-rain:fix_use_snippet_in_types, r=matthiaskrgr
fix some use of `snippet` in `types.rs` changelog: none
2 parents f2ecf39 + f3489d4 commit c057621

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

clippy_lints/src/types.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,15 @@ impl Types {
321321
if let Some(def_id) = res.opt_def_id() {
322322
if Some(def_id) == cx.tcx.lang_items().owned_box() {
323323
if let Some(span) = match_borrows_parameter(cx, qpath) {
324+
let mut applicability = Applicability::MachineApplicable;
324325
span_lint_and_sugg(
325326
cx,
326327
REDUNDANT_ALLOCATION,
327328
hir_ty.span,
328329
"usage of `Box<&T>`",
329330
"try",
330-
snippet(cx, span, "..").to_string(),
331-
Applicability::MachineApplicable,
331+
snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
332+
applicability,
332333
);
333334
return; // don't recurse into the type
334335
}
@@ -345,14 +346,15 @@ impl Types {
345346
}
346347
} else if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
347348
if let Some(span) = match_type_parameter(cx, qpath, &paths::RC) {
349+
let mut applicability = Applicability::MachineApplicable;
348350
span_lint_and_sugg(
349351
cx,
350352
REDUNDANT_ALLOCATION,
351353
hir_ty.span,
352354
"usage of `Rc<Rc<T>>`",
353355
"try",
354-
snippet(cx, span, "..").to_string(),
355-
Applicability::MachineApplicable,
356+
snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
357+
applicability,
356358
);
357359
return; // don't recurse into the type
358360
}
@@ -368,26 +370,31 @@ impl Types {
368370
GenericArg::Type(ty) => ty.span,
369371
_ => return,
370372
};
373+
let mut applicability = Applicability::MachineApplicable;
371374
span_lint_and_sugg(
372375
cx,
373376
REDUNDANT_ALLOCATION,
374377
hir_ty.span,
375378
"usage of `Rc<Box<T>>`",
376379
"try",
377-
format!("Rc<{}>", snippet(cx, inner_span, "..")),
378-
Applicability::MachineApplicable,
380+
format!(
381+
"Rc<{}>",
382+
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
383+
),
384+
applicability,
379385
);
380386
return; // don't recurse into the type
381387
}
382388
if let Some(span) = match_borrows_parameter(cx, qpath) {
389+
let mut applicability = Applicability::MachineApplicable;
383390
span_lint_and_sugg(
384391
cx,
385392
REDUNDANT_ALLOCATION,
386393
hir_ty.span,
387394
"usage of `Rc<&T>`",
388395
"try",
389-
snippet(cx, span, "..").to_string(),
390-
Applicability::MachineApplicable,
396+
snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
397+
applicability,
391398
);
392399
return; // don't recurse into the type
393400
}
@@ -546,7 +553,6 @@ impl Types {
546553
// details.
547554
return;
548555
}
549-
let mut applicability = Applicability::MachineApplicable;
550556
span_lint_and_sugg(
551557
cx,
552558
BORROWED_BOX,
@@ -556,8 +562,12 @@ impl Types {
556562
format!(
557563
"&{}{}",
558564
ltopt,
559-
&snippet_with_applicability(cx, inner.span, "..", &mut applicability)
565+
&snippet(cx, inner.span, "..")
560566
),
567+
// To make this `MachineApplicable`, at least one needs to check if it isn't a trait item
568+
// because the trait impls of it will break otherwise;
569+
// and there may be other cases that result in invalid code.
570+
// For example, type coercion doesn't work nicely.
561571
Applicability::Unspecified,
562572
);
563573
return; // don't recurse into the type

0 commit comments

Comments
 (0)