Skip to content

Commit 3c67feb

Browse files
committed
[useless_conversion]: fix FP in macro and add test
1 parent c01bec6 commit 3c67feb

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

clippy_lints/src/useless_conversion.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ fn into_iter_deep_call<'hir>(cx: &LateContext<'_>, mut expr: &'hir Expr<'hir>) -
102102
(expr, depth)
103103
}
104104

105+
/// Checks if the given `expr` is an argument of a macro invocation.
106+
/// This is a slow-ish operation, so consider calling this late
107+
/// to avoid slowing down the lint in the happy path when not emitting a warning
108+
fn is_macro_argument(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
109+
if let Some(parent) = get_parent_expr(cx, expr) {
110+
parent.span.from_expansion() || is_macro_argument(cx, parent)
111+
} else {
112+
false
113+
}
114+
}
115+
105116
#[expect(clippy::too_many_lines)]
106117
impl<'tcx> LateLintPass<'tcx> for UselessConversion {
107118
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
@@ -174,6 +185,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
174185
&& let Some(&into_iter_param) = sig.inputs().get(kind.param_pos(arg_pos))
175186
&& let ty::Param(param) = into_iter_param.kind()
176187
&& let Some(span) = into_iter_bound(cx, parent_fn_did, into_iter_did, param.index)
188+
&& !is_macro_argument(cx, e)
177189
{
178190
// Get the "innermost" `.into_iter()` call, e.g. given this expression:
179191
// `foo.into_iter().into_iter()`

tests/ui/crashes/ice-11065.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![warn(clippy::useless_conversion)]
2+
3+
use std::iter::FromIterator;
4+
use std::option::IntoIter as OptionIter;
5+
6+
fn eq<T: Eq>(a: T, b: T) -> bool {
7+
a == b
8+
}
9+
10+
macro_rules! tests {
11+
($($expr:expr, $ty:ty, ($($test:expr),*);)+) => (pub fn main() {$({
12+
const C: $ty = $expr;
13+
assert!(eq(C($($test),*), $expr($($test),*)));
14+
})+})
15+
}
16+
17+
tests! {
18+
FromIterator::from_iter, fn(OptionIter<i32>) -> Vec<i32>, (Some(5).into_iter());
19+
}

0 commit comments

Comments
 (0)