Skip to content

Commit

Permalink
Fixing span manipulation and indentation of the suggestion introduced…
Browse files Browse the repository at this point in the history
  • Loading branch information
surechen committed Aug 25, 2024
1 parent 739b1fd commit 8750e24
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4667,10 +4667,15 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
if let hir::ExprKind::Block(b, _) = body.value.kind
&& b.expr.is_none()
{
// The span of '}' in the end of block.
let span = self.tcx.sess.source_map().end_point(b.span);
sugg_spans.push((
// The span will point to the closing curly brace `}` of the block.
b.span.shrink_to_hi().with_lo(b.span.hi() - BytePos(1)),
"\n Ok(())\n}".to_string(),
span.shrink_to_lo(),
format!(
"{}{}",
" Ok(())\n",
self.tcx.sess.source_map().indentation_before(span).unwrap_or_default(),
),
));
}
err.multipart_suggestion_verbose(
Expand Down
18 changes: 6 additions & 12 deletions tests/ui/return/return-from-residual-sugg-issue-125997.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ 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(())
}

Expand All @@ -27,9 +25,8 @@ macro_rules! mac {
let mut _file = File::create("foo.txt")?;
//~^ ERROR the `?` operator can only be used in a function
println!();

Ok(())
}
Ok(())
}
};
}

Expand All @@ -39,23 +36,20 @@ impl A {
fn test4(&self) -> Result<(), Box<dyn std::error::Error>> {
let mut _file = File::create("foo.txt")?;
//~^ ERROR the `?` operator can only be used in a method

Ok(())
}
Ok(())
}

fn test5(&self) -> Result<(), Box<dyn std::error::Error>> {
let mut _file = File::create("foo.txt")?;
//~^ ERROR the `?` operator can only be used in a method
println!();

Ok(())
}
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(())
}
21 changes: 6 additions & 15 deletions tests/ui/return/return-from-residual-sugg-issue-125997.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ 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`)
Expand All @@ -32,9 +30,7 @@ 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 method that returns `Result` or `Option` (or another type that implements `FromResidual`)
Expand All @@ -51,9 +47,8 @@ help: consider adding return type
LL ~ fn test4(&self) -> Result<(), Box<dyn std::error::Error>> {
LL | let mut _file = File::create("foo.txt")?;
LL |
LL ~
LL + Ok(())
LL + }
LL ~ Ok(())
LL ~ }
|

error[E0277]: the `?` operator can only be used in a method that returns `Result` or `Option` (or another type that implements `FromResidual`)
Expand All @@ -71,9 +66,8 @@ LL ~ fn test5(&self) -> Result<(), Box<dyn std::error::Error>> {
LL | let mut _file = File::create("foo.txt")?;
LL |
LL | println!();
LL ~
LL + Ok(())
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`)
Expand All @@ -91,9 +85,7 @@ 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`)
Expand All @@ -115,9 +107,8 @@ LL ~ fn test3() -> Result<(), Box<dyn std::error::Error>> {
LL | let mut _file = File::create("foo.txt")?;
LL |
LL | println!();
LL ~
LL + Ok(())
LL + }
LL ~ Ok(())
LL ~ }
|

error: aborting due to 6 previous errors
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/try-trait/try-operator-on-main.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ 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`
Expand Down

0 comments on commit 8750e24

Please sign in to comment.