Skip to content

Suggest a float literal when dividing a floating-point type by {integer} #93829

Closed
@scottmcm

Description

@scottmcm

(This example is from https://users.rust-lang.org/t/beginner-question-make-a-function-that-divides-a-number-by-100/71577?u=scottmcm )

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=94518a0a52515b56b171089af22d07d9

fn divide_by_one_hundred(x: f32) -> f32 {
    x / 100
}

The current output is:

error[[E0277]](https://doc.rust-lang.org/nightly/error-index.html#E0277): cannot divide `f32` by `{integer}`
 [--> src/lib.rs:2:7
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021#)  |
2 |     x / 100
  |       ^ no implementation for `f32 / {integer}`
  |
  = help: the trait `Div<{integer}>` is not implemented for `f32`

Ideally the output should have a suggested fix, perhaps something like

error[[E0277]](https://doc.rust-lang.org/nightly/error-index.html#E0277): cannot divide `f32` by `{integer}`
 [--> src/lib.rs:2:7
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021#)  |
2 |     x / 100
  |       ^ no implementation for `f32 / {integer}`
  |
  = help: the trait `Div<{integer}>` is not implemented for `f32`
  = suggestion: use a floating-point literal instead
2 |     x / 100.0
            ^^^^^

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions