Skip to content

Commit 5cdba7d

Browse files
committed
Auto merge of rust-lang#7287 - Jarcho:ice_7272, r=flip1995
Fix ICE in `too_many_lines` fixes: rust-lang#7272 fixes: rust-lang#7286 rust-lang#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. rust-lang#7286 is definitely a clippy bug changelog: none
2 parents 543a8a6 + 4ba6afd commit 5cdba7d

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

clippy_lints/src/functions/too_many_lines.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_middle::lint::in_external_macro;
44
use rustc_span::Span;
55

66
use clippy_utils::diagnostics::span_lint;
7-
use clippy_utils::source::snippet;
7+
use clippy_utils::source::snippet_opt;
88

99
use super::TOO_MANY_LINES;
1010

@@ -13,15 +13,25 @@ pub(super) fn check_fn(cx: &LateContext<'_>, span: Span, body: &'tcx hir::Body<'
1313
return;
1414
}
1515

16-
let code_snippet = snippet(cx, body.value.span, "..");
16+
let code_snippet = match snippet_opt(cx, body.value.span) {
17+
Some(s) => s,
18+
_ => return,
19+
};
1720
let mut line_count: u64 = 0;
1821
let mut in_comment = false;
1922
let mut code_in_line;
2023

21-
// Skip the surrounding function decl.
22-
let start_brace_idx = code_snippet.find('{').map_or(0, |i| i + 1);
23-
let end_brace_idx = code_snippet.rfind('}').unwrap_or_else(|| code_snippet.len());
24-
let function_lines = code_snippet[start_brace_idx..end_brace_idx].lines();
24+
let function_lines = if matches!(body.value.kind, hir::ExprKind::Block(..))
25+
&& code_snippet.as_bytes().first().copied() == Some(b'{')
26+
&& code_snippet.as_bytes().last().copied() == Some(b'}')
27+
{
28+
// Removing the braces from the enclosing block
29+
&code_snippet[1..code_snippet.len() - 1]
30+
} else {
31+
&code_snippet
32+
}
33+
.trim() // Remove leading and trailing blank lines
34+
.lines();
2535

2636
for mut line in function_lines {
2737
code_in_line = false;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pub fn warn<T>(_: T) {}
2+
3+
macro_rules! define_macro {
4+
($d:tt $lower:ident $upper:ident) => {
5+
#[macro_export]
6+
macro_rules! $upper {
7+
($arg:tt) => {
8+
$crate::$lower($arg)
9+
};
10+
}
11+
};
12+
}
13+
14+
define_macro! {$ warn WARNING}

tests/ui/crashes/ice-7272.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// aux-build:ice-7272-aux.rs
2+
3+
#![allow(clippy::no_effect)]
4+
5+
extern crate ice_7272_aux;
6+
7+
use ice_7272_aux::*;
8+
9+
pub fn main() {
10+
|| WARNING!("Style changed!");
11+
|| "}{";
12+
}

0 commit comments

Comments
 (0)