diff options
| author | surechen <chenshuo17@huawei.com> | 2024-06-09 17:20:25 +0800 |
|---|---|---|
| committer | surechen <chenshuo17@huawei.com> | 2024-06-12 11:33:22 +0800 |
| commit | 0b3fec9388d358b0b48d1ea2f83ff43468402f23 (patch) | |
| tree | 195b44bd84a9d2bafdb328bb3cf1541df87e6249 /tests/ui | |
| parent | f3ff2f16c8f59fe5db9ec3382e4d2cc913e376a3 (diff) | |
| download | rust-0b3fec9388d358b0b48d1ea2f83ff43468402f23.tar.gz rust-0b3fec9388d358b0b48d1ea2f83ff43468402f23.zip | |
For E0277 suggest adding `Result` return type for function which using QuesionMark `?` in the body.
Diffstat (limited to 'tests/ui')
4 files changed, 172 insertions, 0 deletions
diff --git a/tests/ui/return/return-from-residual-sugg-issue-125997.fixed b/tests/ui/return/return-from-residual-sugg-issue-125997.fixed new file mode 100644 index 00000000000..b2eca69aeb9 --- /dev/null +++ b/tests/ui/return/return-from-residual-sugg-issue-125997.fixed @@ -0,0 +1,42 @@ +//@ 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")?; + //~^ ERROR the `?` operator can only be used in a function + + Ok(()) +} + +fn test2() -> Result<(), Box<dyn std::error::Error>> { + let mut _file = File::create("foo.txt")?; + //~^ ERROR the `?` operator can only be used in a function + println!(); + + Ok(()) +} + +macro_rules! mac { + () => { + fn test3() -> Result<(), Box<dyn std::error::Error>> { + let mut _file = File::create("foo.txt")?; + //~^ ERROR the `?` operator can only be used in a function + println!(); + + Ok(()) +} + }; +} + +fn main() -> Result<(), Box<dyn std::error::Error>> { + let mut _file = File::create("foo.txt")?; + //~^ ERROR the `?` operator can only be used in a function + mac!(); + + Ok(()) +} diff --git a/tests/ui/return/return-from-residual-sugg-issue-125997.rs b/tests/ui/return/return-from-residual-sugg-issue-125997.rs new file mode 100644 index 00000000000..dd8550a388b --- /dev/null +++ b/tests/ui/return/return-from-residual-sugg-issue-125997.rs @@ -0,0 +1,34 @@ +//@ 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 test2() { + let mut _file = File::create("foo.txt")?; + //~^ ERROR the `?` operator can only be used in a function + println!(); +} + +macro_rules! mac { + () => { + fn test3() { + let mut _file = File::create("foo.txt")?; + //~^ ERROR the `?` operator can only be used in a function + println!(); + } + }; +} + +fn main() { + let mut _file = File::create("foo.txt")?; + //~^ ERROR the `?` operator can only be used in a function + mac!(); +} diff --git a/tests/ui/return/return-from-residual-sugg-issue-125997.stderr b/tests/ui/return/return-from-residual-sugg-issue-125997.stderr new file mode 100644 index 00000000000..ef938f0213d --- /dev/null +++ b/tests/ui/return/return-from-residual-sugg-issue-125997.stderr @@ -0,0 +1,86 @@ +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 + +LL + 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 test2() { + | ---------- 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 test2() -> Result<(), Box<dyn std::error::Error>> { +LL | let mut _file = File::create("foo.txt")?; +LL | +LL | println!(); +LL + +LL + 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:31: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 | mac!(); +LL + +LL + 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:23:52 + | +LL | fn test3() { + | ---------- 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 `()` +... +LL | mac!(); + | ------ in this macro invocation + | + = help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()` + = note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info) +help: consider adding return type + | +LL ~ fn test3() -> Result<(), Box<dyn std::error::Error>> { +LL | let mut _file = File::create("foo.txt")?; +LL | +LL | println!(); +LL ~ +LL + Ok(()) +LL + } + | + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/try-trait/try-operator-on-main.stderr b/tests/ui/try-trait/try-operator-on-main.stderr index 7cd38e0cf95..ba6eacde68f 100644 --- a/tests/ui/try-trait/try-operator-on-main.stderr +++ b/tests/ui/try-trait/try-operator-on-main.stderr @@ -8,6 +8,16 @@ LL | std::fs::File::open("foo")?; | ^ 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 | // error for a `Try` type on a non-`Try` fn + ... +LL | try_trait_generic::<()>(); +LL + +LL + Ok(()) +LL + } + | error[E0277]: the `?` operator can only be applied to values that implement `Try` --> $DIR/try-operator-on-main.rs:10:5 |
