Skip to content

RFC: limit suggestions to a white-list of functions or to as_, to_, and into_ methods #42929

Closed
@nikomatsakis

Description

@nikomatsakis

The "here are some functions that may meet your needs" tips are rather broad. I think we should impose some limits. As a spin-off from #42764, consider this (same) example:

fn main() {
    let _: Option<i32> = 42i32;
}

Right now this gives:

error[E0308]: mismatched types
 --> <anon>:2:26
  |
2 |     let _: Option<i32> = 42i32;
  |                          ^^^^^ expected enum `std::option::Option`, found i32
  |
  = note: expected type `std::option::Option<i32>`
             found type `i32`
  = help: here are some functions which might fulfill your needs:
          - .checked_abs()
          - .checked_neg()

checked_abs() and checked_neg() seem like very silly suggestions to me. Similarly, #42746 points out that suggesting unwrap() is sort of inappropriate. I think it would be better if used a whitelist whitelist of approved conversion functions, or else if we limited them to the prefixes as_, to_, or into_. The latter is sort of codifying an existing convention -- I personally see no problem with that, since this is just a compiler hint. It has the plus and minus that it would apply rather broadly (i.e., to functions outside the stdlib), where a whitelist obviously wouldn't unless we standardized it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.I-needs-decisionIssue: In need of a decision.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions