in which the trivial-casts word to the wise is tucked into a help note
The top level message shouldn't be too long; the replaced-by-coercion/temporary-variable advice can live in a note. Also, don't mention type ascription when it's not actually available as a real thing. (The current state of discussion on the type ascription tracking issue #23416 makes one rather suspect it will never be a stable thing in its current form, but that's not for us to adjudicate in this commit.) While we're here, yank out the differentiating parts of the numeric/other conditional and only have one codepath emitting the diagnostic.
This commit is contained in:
parent
a417518173
commit
0b39a82cf4
5 changed files with 115 additions and 21 deletions
|
@ -365,28 +365,27 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
|
||||||
fn trivial_cast_lint(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) {
|
fn trivial_cast_lint(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) {
|
||||||
let t_cast = self.cast_ty;
|
let t_cast = self.cast_ty;
|
||||||
let t_expr = self.expr_ty;
|
let t_expr = self.expr_ty;
|
||||||
if t_cast.is_numeric() && t_expr.is_numeric() {
|
let type_asc_or = if fcx.tcx.features().type_ascription {
|
||||||
fcx.tcx.lint_node(
|
"type ascription or "
|
||||||
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)));
|
|
||||||
} else {
|
} else {
|
||||||
fcx.tcx.lint_node(
|
""
|
||||||
lint::builtin::TRIVIAL_CASTS,
|
};
|
||||||
self.expr.id,
|
let (adjective, lint) = if t_cast.is_numeric() && t_expr.is_numeric() {
|
||||||
self.span,
|
("numeric ", lint::builtin::TRIVIAL_NUMERIC_CASTS)
|
||||||
&format!("trivial cast: `{}` as `{}`. Cast can be \
|
} else {
|
||||||
replaced by coercion, this might require type \
|
("", lint::builtin::TRIVIAL_CASTS)
|
||||||
ascription or a temporary variable",
|
};
|
||||||
fcx.ty_to_string(t_expr),
|
let mut err = fcx.tcx.struct_span_lint_node(
|
||||||
fcx.ty_to_string(t_cast)));
|
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>) {
|
pub fn check(mut self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) {
|
||||||
|
|
20
src/test/ui/lint/trivial-casts-featuring-type-ascription.rs
Normal file
20
src/test/ui/lint/trivial-casts-featuring-type-ascription.rs
Normal file
|
@ -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 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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
|
||||||
|
}
|
|
@ -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
|
||||||
|
|
19
src/test/ui/lint/trivial-casts.rs
Normal file
19
src/test/ui/lint/trivial-casts.rs
Normal file
|
@ -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 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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
|
||||||
|
}
|
28
src/test/ui/lint/trivial-casts.stderr
Normal file
28
src/test/ui/lint/trivial-casts.stderr
Normal file
|
@ -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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue