fix span calculation for non-ascii in needless_return

This commit is contained in:
y21 2024-03-15 15:57:22 +01:00
parent 660b058ba2
commit d3f8f3e9d7
4 changed files with 36 additions and 2 deletions

View File

@ -465,8 +465,8 @@ fn last_statement_borrows<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>)
// Go backwards while encountering whitespace and extend the given Span to that point.
fn extend_span_to_previous_non_ws(cx: &LateContext<'_>, sp: Span) -> Span {
if let Ok(prev_source) = cx.sess().source_map().span_to_prev_source(sp) {
let ws = [' ', '\t', '\n'];
if let Some(non_ws_pos) = prev_source.rfind(|c| !ws.contains(&c)) {
let ws = [b' ', b'\t', b'\n'];
if let Some(non_ws_pos) = prev_source.bytes().rposition(|c| !ws.contains(&c)) {
let len = prev_source.len() - non_ws_pos - 1;
return sp.with_lo(sp.lo() - BytePos::from_usize(len));
}

View File

@ -0,0 +1,7 @@
#![warn(clippy::needless_return)]
fn main() {
if (true) {
// anything一些中文
}
}

View File

@ -0,0 +1,8 @@
#![warn(clippy::needless_return)]
fn main() {
if (true) {
// anything一些中文
return;
}
}

View File

@ -0,0 +1,19 @@
error: unneeded `return` statement
--> tests/ui/crashes/ice-12491.rs:5:24
|
LL | // anything一些中文
| ____________________________^
LL | | return;
| |______________^
|
= note: `-D clippy::needless-return` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::needless_return)]`
help: remove `return`
|
LL - // anything一些中文
LL - return;
LL + // anything一些中文
|
error: aborting due to 1 previous error