Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
doonv opened this issue Apr 28, 2024 · 1 comment · Fixed by #12787
Closed

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

doonv opened this issue Apr 28, 2024 · 1 comment · Fixed by #12787
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@doonv
Copy link

doonv commented Apr 28, 2024

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

@doonv doonv added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Apr 28, 2024
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Apr 28, 2024
@J-ZhengLi
Copy link
Member

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants