diff --git a/src/librustc_typeck/check/cast.rs b/src/librustc_typeck/check/cast.rs index f0d7ca8ebf14f..07e19c84a9507 100644 --- a/src/librustc_typeck/check/cast.rs +++ b/src/librustc_typeck/check/cast.rs @@ -365,28 +365,27 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> { fn trivial_cast_lint(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) { let t_cast = self.cast_ty; let t_expr = self.expr_ty; - if t_cast.is_numeric() && t_expr.is_numeric() { - fcx.tcx.lint_node( - lint::builtin::TRIVIAL_NUMERIC_CASTS, - self.expr.id, - self.span, - &format!("trivial numeric cast: `{}` as `{}`. Cast can be \ - replaced by coercion, this might require type \ - ascription or a temporary variable", - fcx.ty_to_string(t_expr), - fcx.ty_to_string(t_cast))); + let type_asc_or = if fcx.tcx.features().type_ascription { + "type ascription or " } else { - fcx.tcx.lint_node( - lint::builtin::TRIVIAL_CASTS, - self.expr.id, - self.span, - &format!("trivial cast: `{}` as `{}`. Cast can be \ - replaced by coercion, this might require type \ - ascription or a temporary variable", - fcx.ty_to_string(t_expr), - fcx.ty_to_string(t_cast))); - } - + "" + }; + let (adjective, lint) = if t_cast.is_numeric() && t_expr.is_numeric() { + ("numeric ", lint::builtin::TRIVIAL_NUMERIC_CASTS) + } else { + ("", lint::builtin::TRIVIAL_CASTS) + }; + let mut err = fcx.tcx.struct_span_lint_node( + lint, + self.expr.id, + self.span, + &format!("trivial {}cast: `{}` as `{}`", + adjective, + fcx.ty_to_string(t_expr), + fcx.ty_to_string(t_cast))); + err.help(&format!("cast can be replaced by coercion; this might \ + require {}a temporary variable", type_asc_or)); + err.emit(); } pub fn check(mut self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) { diff --git a/src/test/ui/lint/trivial-casts-featuring-type-ascription.rs b/src/test/ui/lint/trivial-casts-featuring-type-ascription.rs new file mode 100644 index 0000000000000..fba3724ae4907 --- /dev/null +++ b/src/test/ui/lint/trivial-casts-featuring-type-ascription.rs @@ -0,0 +1,20 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![deny(trivial_casts, trivial_numeric_casts)] +#![feature(type_ascription)] + +fn main() { + let lugubrious = 12i32 as i32; + //~^ ERROR trivial numeric cast + let haunted: &u32 = &99; + let _ = haunted as *const u32; + //~^ ERROR trivial cast +} diff --git a/src/test/ui/lint/trivial-casts-featuring-type-ascription.stderr b/src/test/ui/lint/trivial-casts-featuring-type-ascription.stderr new file mode 100644 index 0000000000000..a77135c875d79 --- /dev/null +++ b/src/test/ui/lint/trivial-casts-featuring-type-ascription.stderr @@ -0,0 +1,28 @@ +error: trivial numeric cast: `i32` as `i32` + --> $DIR/trivial-casts-featuring-type-ascription.rs:15:22 + | +LL | let lugubrious = 12i32 as i32; + | ^^^^^^^^^^^^ + | +note: lint level defined here + --> $DIR/trivial-casts-featuring-type-ascription.rs:11:24 + | +LL | #![deny(trivial_casts, trivial_numeric_casts)] + | ^^^^^^^^^^^^^^^^^^^^^ + = help: cast can be replaced by coercion; this might require type ascription or a temporary variable + +error: trivial cast: `&u32` as `*const u32` + --> $DIR/trivial-casts-featuring-type-ascription.rs:18:13 + | +LL | let _ = haunted as *const u32; + | ^^^^^^^^^^^^^^^^^^^^^ + | +note: lint level defined here + --> $DIR/trivial-casts-featuring-type-ascription.rs:11:9 + | +LL | #![deny(trivial_casts, trivial_numeric_casts)] + | ^^^^^^^^^^^^^ + = help: cast can be replaced by coercion; this might require type ascription or a temporary variable + +error: aborting due to 2 previous errors + diff --git a/src/test/ui/lint/trivial-casts.rs b/src/test/ui/lint/trivial-casts.rs new file mode 100644 index 0000000000000..759b282c0da92 --- /dev/null +++ b/src/test/ui/lint/trivial-casts.rs @@ -0,0 +1,19 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![deny(trivial_casts, trivial_numeric_casts)] + +fn main() { + let lugubrious = 12i32 as i32; + //~^ ERROR trivial numeric cast + let haunted: &u32 = &99; + let _ = haunted as *const u32; + //~^ ERROR trivial cast +} diff --git a/src/test/ui/lint/trivial-casts.stderr b/src/test/ui/lint/trivial-casts.stderr new file mode 100644 index 0000000000000..d52869f4bed61 --- /dev/null +++ b/src/test/ui/lint/trivial-casts.stderr @@ -0,0 +1,28 @@ +error: trivial numeric cast: `i32` as `i32` + --> $DIR/trivial-casts.rs:14:22 + | +LL | let lugubrious = 12i32 as i32; + | ^^^^^^^^^^^^ + | +note: lint level defined here + --> $DIR/trivial-casts.rs:11:24 + | +LL | #![deny(trivial_casts, trivial_numeric_casts)] + | ^^^^^^^^^^^^^^^^^^^^^ + = help: cast can be replaced by coercion; this might require a temporary variable + +error: trivial cast: `&u32` as `*const u32` + --> $DIR/trivial-casts.rs:17:13 + | +LL | let _ = haunted as *const u32; + | ^^^^^^^^^^^^^^^^^^^^^ + | +note: lint level defined here + --> $DIR/trivial-casts.rs:11:9 + | +LL | #![deny(trivial_casts, trivial_numeric_casts)] + | ^^^^^^^^^^^^^ + = help: cast can be replaced by coercion; this might require a temporary variable + +error: aborting due to 2 previous errors +