Skip to content

Commit 0b39a82

Browse files
committed
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.
1 parent a417518 commit 0b39a82

File tree

5 files changed

+115
-21
lines changed

5 files changed

+115
-21
lines changed

src/librustc_typeck/check/cast.rs

+20-21
Original file line numberDiff line numberDiff line change
@@ -365,28 +365,27 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
365365
fn trivial_cast_lint(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) {
366366
let t_cast = self.cast_ty;
367367
let t_expr = self.expr_ty;
368-
if t_cast.is_numeric() && t_expr.is_numeric() {
369-
fcx.tcx.lint_node(
370-
lint::builtin::TRIVIAL_NUMERIC_CASTS,
371-
self.expr.id,
372-
self.span,
373-
&format!("trivial numeric cast: `{}` as `{}`. Cast can be \
374-
replaced by coercion, this might require type \
375-
ascription or a temporary variable",
376-
fcx.ty_to_string(t_expr),
377-
fcx.ty_to_string(t_cast)));
368+
let type_asc_or = if fcx.tcx.features().type_ascription {
369+
"type ascription or "
378370
} else {
379-
fcx.tcx.lint_node(
380-
lint::builtin::TRIVIAL_CASTS,
381-
self.expr.id,
382-
self.span,
383-
&format!("trivial cast: `{}` as `{}`. Cast can be \
384-
replaced by coercion, this might require type \
385-
ascription or a temporary variable",
386-
fcx.ty_to_string(t_expr),
387-
fcx.ty_to_string(t_cast)));
388-
}
389-
371+
""
372+
};
373+
let (adjective, lint) = if t_cast.is_numeric() && t_expr.is_numeric() {
374+
("numeric ", lint::builtin::TRIVIAL_NUMERIC_CASTS)
375+
} else {
376+
("", lint::builtin::TRIVIAL_CASTS)
377+
};
378+
let mut err = fcx.tcx.struct_span_lint_node(
379+
lint,
380+
self.expr.id,
381+
self.span,
382+
&format!("trivial {}cast: `{}` as `{}`",
383+
adjective,
384+
fcx.ty_to_string(t_expr),
385+
fcx.ty_to_string(t_cast)));
386+
err.help(&format!("cast can be replaced by coercion; this might \
387+
require {}a temporary variable", type_asc_or));
388+
err.emit();
390389
}
391390

392391
pub fn check(mut self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![deny(trivial_casts, trivial_numeric_casts)]
12+
#![feature(type_ascription)]
13+
14+
fn main() {
15+
let lugubrious = 12i32 as i32;
16+
//~^ ERROR trivial numeric cast
17+
let haunted: &u32 = &99;
18+
let _ = haunted as *const u32;
19+
//~^ ERROR trivial cast
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: trivial numeric cast: `i32` as `i32`
2+
--> $DIR/trivial-casts-featuring-type-ascription.rs:15:22
3+
|
4+
LL | let lugubrious = 12i32 as i32;
5+
| ^^^^^^^^^^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/trivial-casts-featuring-type-ascription.rs:11:24
9+
|
10+
LL | #![deny(trivial_casts, trivial_numeric_casts)]
11+
| ^^^^^^^^^^^^^^^^^^^^^
12+
= help: cast can be replaced by coercion; this might require type ascription or a temporary variable
13+
14+
error: trivial cast: `&u32` as `*const u32`
15+
--> $DIR/trivial-casts-featuring-type-ascription.rs:18:13
16+
|
17+
LL | let _ = haunted as *const u32;
18+
| ^^^^^^^^^^^^^^^^^^^^^
19+
|
20+
note: lint level defined here
21+
--> $DIR/trivial-casts-featuring-type-ascription.rs:11:9
22+
|
23+
LL | #![deny(trivial_casts, trivial_numeric_casts)]
24+
| ^^^^^^^^^^^^^
25+
= help: cast can be replaced by coercion; this might require type ascription or a temporary variable
26+
27+
error: aborting due to 2 previous errors
28+

src/test/ui/lint/trivial-casts.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![deny(trivial_casts, trivial_numeric_casts)]
12+
13+
fn main() {
14+
let lugubrious = 12i32 as i32;
15+
//~^ ERROR trivial numeric cast
16+
let haunted: &u32 = &99;
17+
let _ = haunted as *const u32;
18+
//~^ ERROR trivial cast
19+
}

src/test/ui/lint/trivial-casts.stderr

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: trivial numeric cast: `i32` as `i32`
2+
--> $DIR/trivial-casts.rs:14:22
3+
|
4+
LL | let lugubrious = 12i32 as i32;
5+
| ^^^^^^^^^^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/trivial-casts.rs:11:24
9+
|
10+
LL | #![deny(trivial_casts, trivial_numeric_casts)]
11+
| ^^^^^^^^^^^^^^^^^^^^^
12+
= help: cast can be replaced by coercion; this might require a temporary variable
13+
14+
error: trivial cast: `&u32` as `*const u32`
15+
--> $DIR/trivial-casts.rs:17:13
16+
|
17+
LL | let _ = haunted as *const u32;
18+
| ^^^^^^^^^^^^^^^^^^^^^
19+
|
20+
note: lint level defined here
21+
--> $DIR/trivial-casts.rs:11:9
22+
|
23+
LL | #![deny(trivial_casts, trivial_numeric_casts)]
24+
| ^^^^^^^^^^^^^
25+
= help: cast can be replaced by coercion; this might require a temporary variable
26+
27+
error: aborting due to 2 previous errors
28+

0 commit comments

Comments
 (0)