Skip to content

Commit 9538e5b

Browse files
authored
Walk back to the root context to compute the span (rust-lang#14349)
Since the error kind (`io::ErrorKind::other`) is in the root context, the error message must be found in the root context as well to compute the correct span to remove. Fix rust-lang#14346 changelog: [`io_error_other`]: fix non-applicable suggestion r? @llogiq
2 parents 2440f51 + 9275ae9 commit 9538e5b

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

clippy_lints/src/methods/io_other_error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, path: &Expr<'_>, args
2727
"use `std::io::Error::other`",
2828
vec![
2929
(new_segment.ident.span, "other".to_owned()),
30-
(error_kind.span.until(error.span), String::new()),
30+
(error_kind.span.until(error.span.source_callsite()), String::new()),
3131
],
3232
Applicability::MachineApplicable,
3333
);

tests/ui/io_other_error.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,8 @@ mod paths {
5353
fn under_msrv() {
5454
let _err = std::io::Error::new(std::io::ErrorKind::Other, E);
5555
}
56+
57+
pub fn issue14346(x: i32) -> std::io::Error {
58+
std::io::Error::other(format!("{x}"))
59+
//~^ ERROR: this can be `std::io::Error::other(_)`
60+
}

tests/ui/io_other_error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,8 @@ mod paths {
5353
fn under_msrv() {
5454
let _err = std::io::Error::new(std::io::ErrorKind::Other, E);
5555
}
56+
57+
pub fn issue14346(x: i32) -> std::io::Error {
58+
std::io::Error::new(std::io::ErrorKind::Other, format!("{x}"))
59+
//~^ ERROR: this can be `std::io::Error::other(_)`
60+
}

tests/ui/io_other_error.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,17 @@ LL - let _err = io::Error::new(io::ErrorKind::Other, super::E);
4848
LL + let _err = io::Error::other(super::E);
4949
|
5050

51-
error: aborting due to 4 previous errors
51+
error: this can be `std::io::Error::other(_)`
52+
--> tests/ui/io_other_error.rs:58:5
53+
|
54+
LL | std::io::Error::new(std::io::ErrorKind::Other, format!("{x}"))
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56+
|
57+
help: use `std::io::Error::other`
58+
|
59+
LL - std::io::Error::new(std::io::ErrorKind::Other, format!("{x}"))
60+
LL + std::io::Error::other(format!("{x}"))
61+
|
62+
63+
error: aborting due to 5 previous errors
5264

0 commit comments

Comments
 (0)