Tweak -1 as usize
suggestion · rust-lang/rust@86a1946 (original) (raw)
3 files changed
lines changed
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -838,8 +838,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | ||
838 | 838 | }, |
839 | 839 | ) = ex.kind |
840 | 840 | { |
841 | - err.span_suggestion( | |
842 | - ex.span, | |
841 | +let span = if let hir::Node::Expr(parent) = | |
842 | +self.tcx.parent_hir_node(ex.hir_id) | |
843 | + && let hir::ExprKind::Cast(..) = parent.kind | |
844 | +{ | |
845 | +// `-1 as usize` -> `usize::MAX` | |
846 | + parent.span | |
847 | +} else { | |
848 | + ex.span | |
849 | +}; | |
850 | + err.span_suggestion_verbose( | |
851 | + span, | |
843 | 852 | format!( |
844 | 853 | "you may have meant the maximum value of `{actual}`", |
845 | 854 | ), |
| Original file line number | Diff line number | Diff line change | | | ------------------------------------------------------------------------------------- | -------------------------------------------------------- | ----------------------------------------------------------------------------- | | | @@ -2,12 +2,13 @@ error[E0600]: cannot apply unary operator `-` to type `usize` | | | | | 2 | 2 | --> $DIR/feature-gate-negate-unsigned.rs:10:23 | | | 3 | 3 | | | | | 4 | 4 | LL | let _max: usize = -1; | | | 5 | | - | ^^ | | | 6 | | - | | | | 7 | | - | cannot apply unary operator `-` | | | 8 | | - | help: you may have meant the maximum value of `usize`: `usize::MAX` | | | | 5 | + | ^^ cannot apply unary operator `-` | | | | 9 | 6 | | | | | 10 | 7 | = note: unsigned values cannot be negated | | | | 8 | +help: you may have meant the maximum value of `usize` | | | | | 9 | + | | | | | | 10 | +LL | let _max: usize = usize::MAX; | | | | | 11 | + | ~~~~~~~~~~ | | | | 11 | 12 | | | | 12 | 13 | error[E0600]: cannot apply unary operator `-` to type `u8` | | | 13 | 14 | --> $DIR/feature-gate-negate-unsigned.rs:14:14 | |
| Original file line number | Diff line number | Diff line change | | | ------------------------------------------------------------------------------------- | -------------------------------------------------------- | ----------------------------------------------------------------------------- | | | @@ -2,34 +2,37 @@ error[E0600]: cannot apply unary operator `-` to type `usize` | | | | | 2 | 2 | --> $DIR/unsigned-literal-negation.rs:2:13 | | | 3 | 3 | | | | | 4 | 4 | LL | let x = -1 as usize; | | | 5 | | - | ^^ | | | 6 | | - | | | | 7 | | - | cannot apply unary operator `-` | | | 8 | | - | help: you may have meant the maximum value of `usize`: `usize::MAX` | | | | 5 | + | ^^ cannot apply unary operator `-` | | | | 9 | 6 | | | | | 10 | 7 | = note: unsigned values cannot be negated | | | | 8 | +help: you may have meant the maximum value of `usize` | | | | | 9 | + | | | | | | 10 | +LL | let x = usize::MAX; | | | | | 11 | + | ~~~~~~~~~~ | | | | 11 | 12 | | | | 12 | 13 | error[E0600]: cannot apply unary operator `-` to type `usize` | | | 13 | 14 | --> $DIR/unsigned-literal-negation.rs:3:13 | | | 14 | 15 | | | | | 15 | 16 | LL | let x = (-1) as usize; | | | 16 | | - | ^^^^ | | | 17 | | - | | | | 18 | | - | cannot apply unary operator `-` | | | 19 | | - | help: you may have meant the maximum value of `usize`: `usize::MAX` | | | | 17 | + | ^^^^ cannot apply unary operator `-` | | | | 20 | 18 | | | | | 21 | 19 | = note: unsigned values cannot be negated | | | | 20 | +help: you may have meant the maximum value of `usize` | | | | | 21 | + | | | | | | 22 | +LL | let x = usize::MAX; | | | | | 23 | + | ~~~~~~~~~~ | | | | 22 | 24 | | | | 23 | 25 | error[E0600]: cannot apply unary operator `-` to type `u32` | | | 24 | 26 | --> $DIR/unsigned-literal-negation.rs:4:18 | | | 25 | 27 | | | | | 26 | 28 | LL | let x: u32 = -1; | | | 27 | | - | ^^ | | | 28 | | - | | | | 29 | | - | cannot apply unary operator `-` | | | 30 | | - | help: you may have meant the maximum value of `u32`: `u32::MAX` | | | | 29 | + | ^^ cannot apply unary operator `-` | | | | 31 | 30 | | | | | 32 | 31 | = note: unsigned values cannot be negated | | | | 32 | +help: you may have meant the maximum value of `u32` | | | | | 33 | + | | | | | | 34 | +LL | let x: u32 = u32::MAX; | | | | | 35 | + | ~~~~~~~~ | | | | 33 | 36 | | | | 34 | 37 | error: aborting due to 3 previous errors | | | 35 | 38 | | |