Skip to content

Commit c83fd39

Browse files
committed
Resolve conflicts produced by GenericArgs
Addresses the move/zip of Lifetimes and Types vectors from hir::PathParameters into the args vector of GenericArgs
1 parent d9a80d2 commit c83fd39

File tree

9 files changed

+86
-29
lines changed

9 files changed

+86
-29
lines changed

clippy_lints/src/lifetimes.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,15 @@ fn check_fn_inner<'a, 'tcx>(
119119
.expect("a path must have at least one segment")
120120
.args;
121121
if let Some(ref params) = *params {
122-
for bound in &params.lifetimes {
123-
if bound.name.name() != "'static" && !bound.is_elided() {
124-
return;
125-
}
126-
bounds_lts.push(bound);
127-
}
122+
params.args.iter().for_each(|param| match param {
123+
GenericArg::Lifetime(bound) => {
124+
if bound.name.name() != "'static" && !bound.is_elided() {
125+
return;
126+
}
127+
bounds_lts.push(bound);
128+
},
129+
_ => {},
130+
});
128131
}
129132
}
130133
}
@@ -233,9 +236,9 @@ fn could_use_elision<'a, 'tcx: 'a>(
233236
fn allowed_lts_from(named_generics: &[GenericParam]) -> HashSet<RefLt> {
234237
let mut allowed_lts = HashSet::new();
235238
for par in named_generics.iter() {
236-
if let GenericParam::Lifetime(ref lt) = *par {
237-
if lt.bounds.is_empty() {
238-
allowed_lts.insert(RefLt::Named(lt.lifetime.name.name()));
239+
if let GenericParamKind::Lifetime { .. } = par.kind {
240+
if par.bounds.is_empty() {
241+
allowed_lts.insert(RefLt::Named(par.name.name()));
239242
}
240243
}
241244
}
@@ -299,7 +302,11 @@ impl<'v, 't> RefVisitor<'v, 't> {
299302

300303
fn collect_anonymous_lifetimes(&mut self, qpath: &QPath, ty: &Ty) {
301304
if let Some(ref last_path_segment) = last_path_segment(qpath).args {
302-
if !last_path_segment.parenthesized && last_path_segment.lifetimes.is_empty() {
305+
if !last_path_segment.parenthesized
306+
&& !last_path_segment.args.iter().any(|arg| match arg {
307+
GenericArg::Lifetime(_) => true,
308+
GenericArg::Type(_) => false,
309+
}) {
303310
let hir_id = self.cx.tcx.hir.node_to_hir_id(ty.id);
304311
match self.cx.tables.qpath_def(qpath, hir_id) {
305312
Def::TyAlias(def_id) | Def::Struct(def_id) => {
@@ -431,9 +438,11 @@ impl<'tcx> Visitor<'tcx> for LifetimeChecker {
431438
}
432439

433440
fn report_extra_lifetimes<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, func: &'tcx FnDecl, generics: &'tcx Generics) {
434-
let hs = generics
435-
.lifetimes()
436-
.map(|lt| (lt.lifetime.name.name(), lt.lifetime.span))
441+
let hs = generics.params.iter()
442+
.filter_map(|par| match par.kind {
443+
GenericParamKind::Lifetime { .. } => Some((par.name.name(), par.span)),
444+
_ => None,
445+
})
437446
.collect();
438447
let mut checker = LifetimeChecker { map: hs };
439448

clippy_lints/src/methods.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,8 +2101,14 @@ fn is_as_ref_or_mut_trait(ty: &hir::Ty, self_ty: &hir::Ty, generics: &hir::Gener
21012101
if params.parenthesized {
21022102
false
21032103
} else {
2104-
params.types.len() == 1
2105-
&& (is_self_ty(&params.types[0]) || is_ty(&*params.types[0], self_ty))
2104+
// FIXME(flip1995): messy, improve if there is a better option
2105+
// in the compiler
2106+
let types: Vec<_> = params.args.iter().filter_map(|arg| match arg {
2107+
hir::GenericArg::Type(ty) => Some(ty),
2108+
_ => None,
2109+
}).collect();
2110+
types.len() == 1
2111+
&& (is_self_ty(&types[0]) || is_ty(&*types[0], self_ty))
21062112
}
21072113
} else {
21082114
false

clippy_lints/src/needless_pass_by_value.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue {
219219
if let Some(elem_ty) = path.segments.iter()
220220
.find(|seg| seg.name == "Vec")
221221
.and_then(|ps| ps.args.as_ref())
222-
.map(|params| &params.types[0]);
222+
.map(|params| params.args.iter().find_map(|arg| match arg {
223+
GenericArg::Type(ty) => Some(ty),
224+
GenericArg::Lifetime(_) => None,
225+
}).unwrap());
223226
then {
224227
let slice_ty = format!("&[{}]", snippet(cx, elem_ty.span, "_"));
225228
db.span_suggestion(input.span,

clippy_lints/src/ptr.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,14 @@ fn check_fn(cx: &LateContext, decl: &FnDecl, fn_id: NodeId, opt_body_id: Option<
161161
if_chain! {
162162
if let TyPath(QPath::Resolved(_, ref path)) = walk_ptrs_hir_ty(arg).node;
163163
if let Some(&PathSegment{args: Some(ref parameters), ..}) = path.segments.last();
164-
if parameters.types.len() == 1;
165164
then {
166-
ty_snippet = snippet_opt(cx, parameters.types[0].span);
165+
let types: Vec<_> = parameters.args.iter().filter_map(|arg| match arg {
166+
GenericArg::Type(ty) => Some(ty),
167+
_ => None,
168+
}).collect();
169+
if types.len() == 1 {
170+
ty_snippet = snippet_opt(cx, types[0].span);
171+
}
167172
}
168173
};
169174
if let Some(spans) = get_spans(cx, opt_body_id, idx, &[("clone", ".to_owned()")]) {
@@ -220,7 +225,8 @@ fn check_fn(cx: &LateContext, decl: &FnDecl, fn_id: NodeId, opt_body_id: Option<
220225
if let [ref bx] = *pp.segments;
221226
if let Some(ref params) = bx.args;
222227
if !params.parenthesized;
223-
if let [ref inner] = *params.types;
228+
if let [ref inner] = *params.args;
229+
if let GenericArg::Type(inner) = inner;
224230
then {
225231
let replacement = snippet_opt(cx, inner.span);
226232
if let Some(r) = replacement {

clippy_lints/src/transmute.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,10 @@ fn get_type_snippet(cx: &LateContext, path: &QPath, to_ref_ty: Ty) -> String {
457457
if_chain! {
458458
if let Some(ref params) = seg.args;
459459
if !params.parenthesized;
460-
if let Some(to_ty) = params.types.get(1);
460+
if let Some(to_ty) = params.args.iter().filter_map(|arg| match arg {
461+
GenericArg::Type(ty) => Some(ty),
462+
GenericArg::Lifetime(_) => None,
463+
}).nth(1);
461464
if let TyRptr(_, ref to_ty) = to_ty.node;
462465
then {
463466
return snippet(cx, to_ty.ty.span, &to_ref_ty.to_string()).to_string();

clippy_lints/src/types.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,10 @@ fn match_type_parameter(cx: &LateContext, qpath: &QPath, path: &[&str]) -> bool
182182
if_chain! {
183183
if let Some(ref params) = last.args;
184184
if !params.parenthesized;
185-
if let Some(ty) = params.types.get(0);
185+
if let Some(ty) = params.args.iter().find_map(|arg| match arg {
186+
GenericArg::Type(ty) => Some(ty),
187+
GenericArg::Lifetime(_) => None,
188+
});
186189
if let TyPath(ref qpath) = ty.node;
187190
if let Some(did) = opt_def_id(cx.tables.qpath_def(qpath, cx.tcx.hir.node_to_hir_id(ty.id)));
188191
if match_def_path(cx.tcx, did, path);
@@ -246,22 +249,33 @@ fn check_ty(cx: &LateContext, ast_ty: &hir::Ty, is_local: bool) {
246249
for ty in p.segments.iter().flat_map(|seg| {
247250
seg.args
248251
.as_ref()
249-
.map_or_else(|| [].iter(), |params| params.types.iter())
252+
.map_or_else(|| [].iter(), |params| params.args.iter())
253+
.filter_map(|arg| match arg {
254+
GenericArg::Type(ty) => Some(ty),
255+
GenericArg::Lifetime(_) => None,
256+
})
250257
}) {
251258
check_ty(cx, ty, is_local);
252259
}
253260
},
254261
QPath::Resolved(None, ref p) => for ty in p.segments.iter().flat_map(|seg| {
255262
seg.args
256263
.as_ref()
257-
.map_or_else(|| [].iter(), |params| params.types.iter())
264+
.map_or_else(|| [].iter(), |params| params.args.iter())
265+
.filter_map(|arg| match arg {
266+
GenericArg::Type(ty) => Some(ty),
267+
GenericArg::Lifetime(_) => None,
268+
})
258269
}) {
259270
check_ty(cx, ty, is_local);
260271
},
261272
QPath::TypeRelative(ref ty, ref seg) => {
262273
check_ty(cx, ty, is_local);
263274
if let Some(ref params) = seg.args {
264-
for ty in params.types.iter() {
275+
for ty in params.args.iter().filter_map(|arg| match arg {
276+
GenericArg::Type(ty) => Some(ty),
277+
GenericArg::Lifetime(_) => None,
278+
}) {
265279
check_ty(cx, ty, is_local);
266280
}
267281
}
@@ -290,7 +304,8 @@ fn check_ty_rptr(cx: &LateContext, ast_ty: &hir::Ty, is_local: bool, lt: &Lifeti
290304
if let [ref bx] = *path.segments;
291305
if let Some(ref params) = bx.args;
292306
if !params.parenthesized;
293-
if let [ref inner] = *params.types;
307+
if let [ref inner] = *params.args;
308+
if let GenericArg::Type(inner) = inner;
294309
then {
295310
if is_any_trait(inner) {
296311
// Ignore `Box<Any>` types, see #1884 for details.
@@ -1862,7 +1877,11 @@ impl<'tcx> ImplicitHasherType<'tcx> {
18621877
/// Checks that `ty` is a target type without a BuildHasher.
18631878
fn new<'a>(cx: &LateContext<'a, 'tcx>, hir_ty: &hir::Ty) -> Option<Self> {
18641879
if let TyPath(QPath::Resolved(None, ref path)) = hir_ty.node {
1865-
let params = &path.segments.last().as_ref()?.args.as_ref()?.types;
1880+
let params: Vec<_> = path.segments.last().as_ref()?.args.as_ref()?
1881+
.args.iter().filter_map(|arg| match arg {
1882+
GenericArg::Type(ty) => Some(ty),
1883+
GenericArg::Lifetime(_) => None,
1884+
}).collect();
18661885
let params_len = params.len();
18671886

18681887
let ty = hir_ty_to_ty(cx.tcx, hir_ty);

clippy_lints/src/use_self.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
6060
then {
6161
let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).args;
6262
let should_check = if let Some(ref params) = *parameters {
63-
!params.parenthesized && params.lifetimes.len() == 0
63+
!params.parenthesized && !params.args.iter().any(|arg| match arg {
64+
GenericArg::Lifetime(_) => true,
65+
GenericArg::Type(_) => false,
66+
})
6467
} else {
6568
true
6669
};

clippy_lints/src/utils/hir_utils.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
152152
left.ident.name == right.ident.name && self.eq_expr(&left.expr, &right.expr)
153153
}
154154

155+
fn eq_generic_arg(&mut self, left: &GenericArg, right: &GenericArg) -> bool {
156+
match (left, right) {
157+
(GenericArg::Lifetime(l_lt), GenericArg::Lifetime(r_lt)) => self.eq_lifetime(l_lt, r_lt),
158+
(GenericArg::Type(l_ty), GenericArg::Type(r_ty)) => self.eq_ty(l_ty, r_ty),
159+
_ => false,
160+
}
161+
}
162+
155163
fn eq_lifetime(&mut self, left: &Lifetime, right: &Lifetime) -> bool {
156164
left.name == right.name
157165
}
@@ -203,8 +211,7 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
203211

204212
fn eq_path_parameters(&mut self, left: &GenericArgs, right: &GenericArgs) -> bool {
205213
if !(left.parenthesized || right.parenthesized) {
206-
over(&left.lifetimes, &right.lifetimes, |l, r| self.eq_lifetime(l, r))
207-
&& over(&left.types, &right.types, |l, r| self.eq_ty(l, r))
214+
over(&left.args, &right.args, |l, r| self.eq_generic_arg(l, r)) // FIXME(flip1995): may not work
208215
&& over(&left.bindings, &right.bindings, |l, r| self.eq_type_binding(l, r))
209216
} else if left.parenthesized && right.parenthesized {
210217
over(left.inputs(), right.inputs(), |l, r| self.eq_ty(l, r))

clippy_lints/src/utils/sugg.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ impl<'a> Sugg<'a> {
100100
ast::ExprKind::ObsoleteInPlace(..) |
101101
ast::ExprKind::Unary(..) |
102102
ast::ExprKind::Match(..) => Sugg::MaybeParen(snippet),
103+
ast::ExprKind::Async(..) |
103104
ast::ExprKind::Block(..) |
104105
ast::ExprKind::Break(..) |
105106
ast::ExprKind::Call(..) |

0 commit comments

Comments
 (0)