Skip to content

Commit 3f6b29a

Browse files
committed
[impl_trait_in_params]: fix span calculation
1 parent a4b2864 commit 3f6b29a

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

clippy_lints/src/functions/impl_trait_in_params.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,10 @@ use rustc_hir as hir;
55
use rustc_hir::intravisit::FnKind;
66
use rustc_hir::{Body, GenericParam, Generics, HirId, ImplItem, ImplItemKind, TraitItem, TraitItemKind};
77
use rustc_lint::LateContext;
8-
use rustc_span::symbol::Ident;
9-
use rustc_span::{BytePos, Span};
108

119
use super::IMPL_TRAIT_IN_PARAMS;
1210

13-
fn report(
14-
cx: &LateContext<'_>,
15-
param: &GenericParam<'_>,
16-
ident: &Ident,
17-
generics: &Generics<'_>,
18-
first_param_span: Span,
19-
) {
11+
fn report(cx: &LateContext<'_>, param: &GenericParam<'_>, generics: &Generics<'_>) {
2012
// No generics with nested generics, and no generics like FnMut(x)
2113
span_lint_and_then(
2214
cx,
@@ -35,12 +27,7 @@ fn report(
3527
);
3628
} else {
3729
diag.span_suggestion_with_style(
38-
Span::new(
39-
first_param_span.lo() - rustc_span::BytePos(1),
40-
ident.span.hi(),
41-
ident.span.ctxt(),
42-
ident.span.parent(),
43-
),
30+
generics.span,
4431
"add a type parameter",
4532
format!("<{{ /* Generic name */ }}: {}>", &param.name.ident().as_str()[5..]),
4633
rustc_errors::Applicability::HasPlaceholders,
@@ -52,13 +39,13 @@ fn report(
5239
}
5340

5441
pub(super) fn check_fn<'tcx>(cx: &LateContext<'_>, kind: &'tcx FnKind<'_>, body: &'tcx Body<'_>, hir_id: HirId) {
55-
if let FnKind::ItemFn(ident, generics, _) = kind
42+
if let FnKind::ItemFn(_, generics, _) = kind
5643
&& cx.tcx.visibility(cx.tcx.hir().body_owner_def_id(body.id())).is_public()
5744
&& !is_in_test_function(cx.tcx, hir_id)
5845
{
5946
for param in generics.params {
6047
if param.is_impl_trait() {
61-
report(cx, param, ident, generics, body.params[0].span);
48+
report(cx, param, generics);
6249
};
6350
}
6451
}
@@ -76,7 +63,7 @@ pub(super) fn check_impl_item(cx: &LateContext<'_>, impl_item: &ImplItem<'_>) {
7663
{
7764
for param in impl_item.generics.params {
7865
if param.is_impl_trait() {
79-
report(cx, param, &impl_item.ident, impl_item.generics, body.params[0].span);
66+
report(cx, param, impl_item.generics);
8067
}
8168
}
8269
}
@@ -92,8 +79,7 @@ pub(super) fn check_trait_item(cx: &LateContext<'_>, trait_item: &TraitItem<'_>,
9279
{
9380
for param in trait_item.generics.params {
9481
if param.is_impl_trait() {
95-
let sp = trait_item.ident.span.with_hi(trait_item.ident.span.hi() + BytePos(1));
96-
report(cx, param, &trait_item.ident, trait_item.generics, sp.shrink_to_hi());
82+
report(cx, param, trait_item.generics);
9783
}
9884
}
9985
}

tests/ui/crashes/ice-11803.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@no-rustfix
2+
3+
#![warn(clippy::impl_trait_in_params)]
4+
5+
pub fn g<T: IntoIterator<Item = impl Iterator<Item = impl Clone>>>() {
6+
extern "C" fn implementation_detail() {}
7+
}
8+
9+
fn main() {}

tests/ui/crashes/ice-11803.stderr

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: `impl Trait` used as a function parameter
2+
--> $DIR/ice-11803.rs:5:54
3+
|
4+
LL | pub fn g<T: IntoIterator<Item = impl Iterator<Item = impl Clone>>>() {
5+
| ^^^^^^^^^^
6+
|
7+
= note: `-D clippy::impl-trait-in-params` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::impl_trait_in_params)]`
9+
help: add a type parameter
10+
|
11+
LL | pub fn g<T: IntoIterator<Item = impl Iterator<Item = impl Clone>>, { /* Generic name */ }: Clone>() {
12+
| +++++++++++++++++++++++++++++++
13+
14+
error: `impl Trait` used as a function parameter
15+
--> $DIR/ice-11803.rs:5:33
16+
|
17+
LL | pub fn g<T: IntoIterator<Item = impl Iterator<Item = impl Clone>>>() {
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
|
20+
help: add a type parameter
21+
|
22+
LL | pub fn g<T: IntoIterator<Item = impl Iterator<Item = impl Clone>>, { /* Generic name */ }: Iterator<Item = impl Clone>>() {
23+
| +++++++++++++++++++++++++++++++++++++++++++++++++++++
24+
25+
error: aborting due to 2 previous errors
26+

0 commit comments

Comments
 (0)