Skip to content

from_str_radix_10 lint shows up in const contexts when the suggestion is not const #12731

Closed
@doonv

Description

@doonv

Summary

Using {integer}::from_str_radix(string, 10) (Using const_int_from_str feature) in const contexts causes Clippy to suggest using string.parse::<{integer}>(), which is not possible in const contexts.

Lint Name

from_str_radix_10

Reproducer

I tried this code:

#![feature(const_int_from_str)]

const _: () = {
    let _ = u32::from_str_radix("123", 10);
};

I saw this happen:

warning: this call to `from_str_radix` can be replaced with a call to `str::parse`
 --> src/lib.rs:4:13
  |
4 |     let _ = u32::from_str_radix("123", 10);
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `"123".parse::<u32>()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#from_str_radix_10
  = note: `#[warn(clippy::from_str_radix_10)]` on by default

I expected to see this happen: nothing

When trying suggestion:

const _: () = {
    let _ = "123".parse::<u32>();
};

This happens:

error[E0015]: cannot call non-const fn `core::str::<impl str>::parse::<u32>` in constants
 --> src/lib.rs:2:19
  |
4 |     let _ = "123".parse::<u32>();
  |                   ^^^^^^^^^^^^^^
  |
  = note: calls in constants are limited to constant functions, tuple structs and tuple variants

Version

rustc 1.79.0-nightly (aed2187d5 2024-04-27)
binary: rustc
commit-hash: aed2187d53b8789e3a37f50ae36f894a2a679077
commit-date: 2024-04-27
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.4

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions