Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix span calculation for non-ascii in needless_return #12493

Merged
merged 1 commit into from Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions clippy_lints/src/returns.rs
Expand Up @@ -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));
}
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/crashes/ice-12491.fixed
@@ -0,0 +1,7 @@
#![warn(clippy::needless_return)]

fn main() {
if (true) {
// anything一些中文
}
}
8 changes: 8 additions & 0 deletions tests/ui/crashes/ice-12491.rs
@@ -0,0 +1,8 @@
#![warn(clippy::needless_return)]

fn main() {
if (true) {
// anything一些中文
return;
}
}
19 changes: 19 additions & 0 deletions tests/ui/crashes/ice-12491.stderr
@@ -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