Skip to content

Commit 4b68c96

Browse files
author
Joshua Holmer
committed
Resolve ICE in needless range loop lint
An ICE would occur if the needless range loop was triggered within a procedural macro, because Clippy would try to produce a code suggestion which was invalid, and caused the compiler to crash. This commit takes the same approach which Clippy currently takes to work around this type of crash in the needless pass by value lint, which is to skip the lint if Clippy is inside of a macro.
1 parent 6ae89c4 commit 4b68c96

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

clippy_lints/src/loops.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use std::mem;
3131
use crate::syntax::ast;
3232
use crate::syntax::source_map::Span;
3333
use crate::syntax_pos::BytePos;
34-
use crate::utils::{sugg, sext};
34+
use crate::utils::{in_macro, sugg, sext};
3535
use crate::utils::usage::mutated_variables;
3636
use crate::consts::{constant, Constant};
3737

@@ -1030,6 +1030,10 @@ fn check_for_loop_range<'a, 'tcx>(
10301030
body: &'tcx Expr,
10311031
expr: &'tcx Expr,
10321032
) {
1033+
if in_macro(expr.span) {
1034+
return;
1035+
}
1036+
10331037
if let Some(higher::Range {
10341038
start: Some(start),
10351039
ref end,

mini-macro/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@ use proc_macro::{TokenStream, quote};
1717
pub fn mini_macro(_: TokenStream) -> TokenStream {
1818
quote!(
1919
#[allow(unused)] fn needless_take_by_value(s: String) { println!("{}", s.len()); }
20+
#[allow(unused)] fn needless_loop(items: &[u8]) {
21+
for i in 0..items.len() {
22+
println!("{}", items[i]);
23+
}
24+
}
2025
)
2126
}

0 commit comments

Comments
 (0)