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

Tweak -1 as usize suggestion #127349

Merged
merged 1 commit into from
Jul 5, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions compiler/rustc_hir_typeck/src/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
},
) = ex.kind
{
err.span_suggestion(
ex.span,
let span = if let hir::Node::Expr(parent) =
self.tcx.parent_hir_node(ex.hir_id)
petrochenkov marked this conversation as resolved.
Show resolved Hide resolved
&& let hir::ExprKind::Cast(..) = parent.kind
{
// `-1 as usize` -> `usize::MAX`
parent.span
} else {
ex.span
};
err.span_suggestion_verbose(
span,
format!(
"you may have meant the maximum value of `{actual}`",
),
Expand Down
9 changes: 5 additions & 4 deletions tests/ui/feature-gates/feature-gate-negate-unsigned.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ error[E0600]: cannot apply unary operator `-` to type `usize`
--> $DIR/feature-gate-negate-unsigned.rs:10:23
|
LL | let _max: usize = -1;
| ^^
| |
| cannot apply unary operator `-`
| help: you may have meant the maximum value of `usize`: `usize::MAX`
| ^^ cannot apply unary operator `-`
|
= note: unsigned values cannot be negated
help: you may have meant the maximum value of `usize`
|
LL | let _max: usize = usize::MAX;
| ~~~~~~~~~~

error[E0600]: cannot apply unary operator `-` to type `u8`
--> $DIR/feature-gate-negate-unsigned.rs:14:14
Expand Down
27 changes: 15 additions & 12 deletions tests/ui/unsigned-literal-negation.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,37 @@ error[E0600]: cannot apply unary operator `-` to type `usize`
--> $DIR/unsigned-literal-negation.rs:2:13
|
LL | let x = -1 as usize;
| ^^
| |
| cannot apply unary operator `-`
| help: you may have meant the maximum value of `usize`: `usize::MAX`
| ^^ cannot apply unary operator `-`
|
= note: unsigned values cannot be negated
help: you may have meant the maximum value of `usize`
|
LL | let x = usize::MAX;
| ~~~~~~~~~~

error[E0600]: cannot apply unary operator `-` to type `usize`
--> $DIR/unsigned-literal-negation.rs:3:13
|
LL | let x = (-1) as usize;
| ^^^^
| |
| cannot apply unary operator `-`
| help: you may have meant the maximum value of `usize`: `usize::MAX`
| ^^^^ cannot apply unary operator `-`
|
= note: unsigned values cannot be negated
help: you may have meant the maximum value of `usize`
|
LL | let x = usize::MAX;
| ~~~~~~~~~~

error[E0600]: cannot apply unary operator `-` to type `u32`
--> $DIR/unsigned-literal-negation.rs:4:18
|
LL | let x: u32 = -1;
| ^^
| |
| cannot apply unary operator `-`
| help: you may have meant the maximum value of `u32`: `u32::MAX`
| ^^ cannot apply unary operator `-`
|
= note: unsigned values cannot be negated
help: you may have meant the maximum value of `u32`
|
LL | let x: u32 = u32::MAX;
| ~~~~~~~~

error: aborting due to 3 previous errors

Expand Down
Loading