; let _ = 2 as Option; before it looks like this e...">

Improved diagnostics for non-primitive cast on non-primitive types (Arc, Option) by Kivooeo · Pull Request #140196 · rust-lang/rust (original) (raw)

here is a small fix that improving error messaging when user is trying to do something like this

let _ = "x" as Arc; let _ = 2 as Option;

before it looks like this

error[E0605]: non-primitive cast: &'static str as Arc<str> --> src\main.rs:3:13 | 3 | let _ = "x" as Arc; | ^^^^^^^^^^^^^^^ help: consider using the From trait instead: Arc<str>::from("x")

error[E0605]: non-primitive cast: i32 as Option<i32> --> src\main.rs:4:13 | 4 | let _ = 2 as Option;

which looks horrible to be honest
so i made a small fix that make errors looks like this

error[E0605]: non-primitive cast: &'static str as Arc<str> | 3 | let _ = "x" as Arc; | ^^^^^^^^^^^^^^^ | = note: an as expression can only be used to convert between primitive types or to coerce to a specific trait object help: consider using the From trait instead | 3 - let _ = "x" as Arc; 3 + let _ = Arc::::from("x"); |

error[E0605]: non-primitive cast: i32 as Option<i32> | 4 | let _ = 2 as Option; | ^^^^^^^^^^^^^^^^ | = note: an as expression can only be used to convert between primitive types or to coerce to a specific trait object help: consider using the From trait instead | 4 - let _ = 2 as Option; 4 + let _ = Option::::from(2);

What improves?

  1. Arc<str>::from("x") which makes no sense because of missing ::
  2. readability

Related Issue
fixes #135412