Auto merge of #7287 - Jarcho:ice_7272, r=flip1995

Fix ICE in `too_many_lines`

fixes: #7272
fixes: #7286

#7272 looks like it's caused by a bug in rust-c. The span it's giving for the closure is:

```rust
$crate:: $lower($d arg) }
        }
    }
}
```

The correct span would be `$crate:: $lower($d arg)` without all the closing braces.

#7286 is definitely a clippy bug

changelog: none
This commit is contained in:
bors 2021-05-28 09:22:22 +00:00
commit 5cdba7d08a
3 changed files with 42 additions and 6 deletions

View File

@ -4,7 +4,7 @@ use rustc_middle::lint::in_external_macro;
use rustc_span::Span;
use clippy_utils::diagnostics::span_lint;
use clippy_utils::source::snippet;
use clippy_utils::source::snippet_opt;
use super::TOO_MANY_LINES;
@ -13,15 +13,25 @@ pub(super) fn check_fn(cx: &LateContext<'_>, span: Span, body: &'tcx hir::Body<'
return;
}
let code_snippet = snippet(cx, body.value.span, "..");
let code_snippet = match snippet_opt(cx, body.value.span) {
Some(s) => s,
_ => return,
};
let mut line_count: u64 = 0;
let mut in_comment = false;
let mut code_in_line;
// Skip the surrounding function decl.
let start_brace_idx = code_snippet.find('{').map_or(0, |i| i + 1);
let end_brace_idx = code_snippet.rfind('}').unwrap_or_else(|| code_snippet.len());
let function_lines = code_snippet[start_brace_idx..end_brace_idx].lines();
let function_lines = if matches!(body.value.kind, hir::ExprKind::Block(..))
&& code_snippet.as_bytes().first().copied() == Some(b'{')
&& code_snippet.as_bytes().last().copied() == Some(b'}')
{
// Removing the braces from the enclosing block
&code_snippet[1..code_snippet.len() - 1]
} else {
&code_snippet
}
.trim() // Remove leading and trailing blank lines
.lines();
for mut line in function_lines {
code_in_line = false;

View File

@ -0,0 +1,14 @@
pub fn warn<T>(_: T) {}
macro_rules! define_macro {
($d:tt $lower:ident $upper:ident) => {
#[macro_export]
macro_rules! $upper {
($arg:tt) => {
$crate::$lower($arg)
};
}
};
}
define_macro! {$ warn WARNING}

View File

@ -0,0 +1,12 @@
// aux-build:ice-7272-aux.rs
#![allow(clippy::no_effect)]
extern crate ice_7272_aux;
use ice_7272_aux::*;
pub fn main() {
|| WARNING!("Style changed!");
|| "}{";
}