about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJoshua Holmer <jholmer@dminc.com>2018-10-17 10:43:32 -0400
committerJoshua Holmer <jholmer@dminc.com>2018-10-17 10:43:32 -0400
commit4b68c965feec8ce0e86d0b396baca5e33c3cf0af (patch)
tree956be266761be3da344ce9b2bc9a28a2b157ad6f
parent6ae89c4f1123d81e56141b02cd3b83436c1ad035 (diff)
downloadrust-4b68c965feec8ce0e86d0b396baca5e33c3cf0af.tar.gz
rust-4b68c965feec8ce0e86d0b396baca5e33c3cf0af.zip
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.
-rw-r--r--clippy_lints/src/loops.rs6
-rw-r--r--mini-macro/src/lib.rs5
2 files changed, 10 insertions, 1 deletions
diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs
index 3c4f06077d9..950c1043802 100644
--- a/clippy_lints/src/loops.rs
+++ b/clippy_lints/src/loops.rs
@@ -31,7 +31,7 @@ use std::mem;
 use crate::syntax::ast;
 use crate::syntax::source_map::Span;
 use crate::syntax_pos::BytePos;
-use crate::utils::{sugg, sext};
+use crate::utils::{in_macro, sugg, sext};
 use crate::utils::usage::mutated_variables;
 use crate::consts::{constant, Constant};
 
@@ -1030,6 +1030,10 @@ fn check_for_loop_range<'a, 'tcx>(
     body: &'tcx Expr,
     expr: &'tcx Expr,
 ) {
+    if in_macro(expr.span) {
+        return;
+    }
+
     if let Some(higher::Range {
         start: Some(start),
         ref end,
diff --git a/mini-macro/src/lib.rs b/mini-macro/src/lib.rs
index d326dd7e679..b6405975862 100644
--- a/mini-macro/src/lib.rs
+++ b/mini-macro/src/lib.rs
@@ -17,5 +17,10 @@ use proc_macro::{TokenStream, quote};
 pub fn mini_macro(_: TokenStream) -> TokenStream {
     quote!(
         #[allow(unused)] fn needless_take_by_value(s: String) { println!("{}", s.len()); }
+        #[allow(unused)] fn needless_loop(items: &[u8]) {
+            for i in 0..items.len() {
+                println!("{}", items[i]);
+            }
+        }
     )
 }