-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For E0277 suggest adding
Result
return type for function which usin…
…g QuesionMark `?` in the body. fixes #125997
- Loading branch information
Showing
6 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
tests/ui/return/return-from-residual-sugg-issue-125997.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//@ run-rustfix | ||
|
||
#![allow(unused_imports)] | ||
#![allow(dead_code)] | ||
|
||
use std::fs::File; | ||
use std::io::prelude::*; | ||
|
||
fn test1() -> Result<(), Box<dyn std::error::Error>> { | ||
let mut _file = File::create("foo.txt")?; | ||
|
||
return Ok(()); | ||
} | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let mut _file = File::create("foo.txt")?; | ||
|
||
return Ok(()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//@ run-rustfix | ||
|
||
#![allow(unused_imports)] | ||
#![allow(dead_code)] | ||
|
||
use std::fs::File; | ||
use std::io::prelude::*; | ||
|
||
fn test1() { | ||
let mut _file = File::create("foo.txt")?; | ||
//~^ ERROR the `?` operator can only be used in a function | ||
} | ||
|
||
fn main() { | ||
let mut _file = File::create("foo.txt")?; | ||
//~^ ERROR the `?` operator can only be used in a function | ||
} |
39 changes: 39 additions & 0 deletions
39
tests/ui/return/return-from-residual-sugg-issue-125997.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) | ||
--> $DIR/return-from-residual-sugg-issue-125997.rs:10:44 | ||
| | ||
LL | fn test1() { | ||
| ---------- this function should return `Result` or `Option` to accept `?` | ||
LL | let mut _file = File::create("foo.txt")?; | ||
| ^ cannot use the `?` operator in a function that returns `()` | ||
| | ||
= help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()` | ||
help: consider adding return type | ||
| | ||
LL ~ fn test1() -> Result<(), Box<dyn std::error::Error>> { | ||
LL ~ let mut _file = File::create("foo.txt")?; | ||
LL + | ||
LL + return Ok(()); | ||
LL + } | ||
| | ||
|
||
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) | ||
--> $DIR/return-from-residual-sugg-issue-125997.rs:15:44 | ||
| | ||
LL | fn main() { | ||
| --------- this function should return `Result` or `Option` to accept `?` | ||
LL | let mut _file = File::create("foo.txt")?; | ||
| ^ cannot use the `?` operator in a function that returns `()` | ||
| | ||
= help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()` | ||
help: consider adding return type | ||
| | ||
LL ~ fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
LL ~ let mut _file = File::create("foo.txt")?; | ||
LL + | ||
LL + return Ok(()); | ||
LL + } | ||
| | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters