Change:
Code: Playground: Error message: Somehow, in one case this in the expression is unsigned, but in the other case it is signed and everything is fine. <!-- TRIAGEBOTSTART --<!-- TRIAGEBOTASSIGNSTART --<!-- TRIAGEBOTASSIGNDATASTARTTRIAGEBOTASSIGNDATAEND --<!-- TRIAGEBOTASSIGNEND --<!-- TRIAGEBOTEND --
For the compiler has to suggest to replace it with . The compiler should also suggest to replace with , to remove some hard "as" casts from the code.
claim
I think this is more appropriate as a clippy lint, opened

Discussion:
use super::method::MethodCallee; use super::FnCtxt; <ins> use rustc_ast as ast; </ins> use rustc_errors::{self, struct_span_err, Applicability, DiagnosticBuilder}; use rustc_hir as hir; use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};

use rustc_middle::ty::{ self, suggest_constraining_type_param, Ty, TyCtxt, TypeFoldable, TypeVisitor, }; <ins> use rustc_span::source_map::Spanned; </ins> use rustc_span::symbol::{sym, Ident}; use rustc_span::Span; use rustc_trait_selection::infer::InferCtxtExt;

match actual.kind() { Uint(_) if op == hir::UnOp::UnNeg => { err.note("unsigned values cannot be negated"); <ins> if let hir::ExprKind::Unary( _, hir::Expr { kind: hir::ExprKind::Lit(Spanned { node: ast::LitKind::Int(1, _), .. }), .. }, ) = ex.kind { err.span_suggestion( ex.span, &format!( "you may have meant the maximum value of `{}`", actual ), format!("{}::MAX", actual), Applicability::MaybeIncorrect, ); } </ins> } Str | Never | Char | Tuple(_) | Array(_, _) => {} Ref(_, ref lty, _) if *lty.kind() == Str => {}

--> $DIR/feature-gate-negate-unsigned.rs:10:23 | LL |     let _max: usize = -1; <del> |                       ^^ cannot apply unary operator `-` </del> <ins> |                       ^^ |                       | |                       cannot apply unary operator `-` |                       help: you may have meant the maximum value of `usize`: `usize::MAX` </ins> | = note: unsigned values cannot be negated

<ins> fn main() { let x = -1 as usize; //~ ERROR: cannot apply unary operator `-` let x = (-1) as usize; //~ ERROR: cannot apply unary operator `-` let x: u32 = -1; //~ ERROR: cannot apply unary operator `-` } </ins>

<ins> 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` | = note: unsigned values cannot be negated 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` | = note: unsigned values cannot be negated 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` | = note: unsigned values cannot be negated error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0600`. </ins>