mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-22 20:53:37 +00:00
Fix ICE in too_many_lines
due to wrong assumptions on braces.
This commit is contained in:
parent
543a8a6aac
commit
4ba6afd192
@ -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;
|
||||
|
14
tests/ui/crashes/auxiliary/ice-7272-aux.rs
Normal file
14
tests/ui/crashes/auxiliary/ice-7272-aux.rs
Normal 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}
|
12
tests/ui/crashes/ice-7272.rs
Normal file
12
tests/ui/crashes/ice-7272.rs
Normal 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!");
|
||||
|| "}{";
|
||||
}
|
Loading…
Reference in New Issue
Block a user