Conversation
|
Thanks for the pull request, and welcome! You should hear from one of our reviewers after this PR gets at least 2 reviews from the community. Please see the contribution instructions for more information. |
|
|
||
| NonZeroU16::from_str_radix("8", 10)?; | ||
| //~^ from_str_radix_10 | ||
|
|
||
| std::num::NonZeroI32::from_str_radix("42", 10)?; | ||
| //~^ from_str_radix_10 | ||
|
|
||
| MyNonZero::from_str_radix("11", 10)?; | ||
| //~^ from_str_radix_10 |
There was a problem hiding this comment.
Please move to fn issue_XYZ like the rest.
fn main is for the initial impl.
There was a problem hiding this comment.
Done in 8084d7a. I moved the NonZero cases into issue_17712; main remains for the initial implementation.
| ty.span, | ||
| exp.span.ctxt(), | ||
| "<integer>", | ||
| &mut Applicability::MaybeIncorrect, |
There was a problem hiding this comment.
could you add an comment why this may be incorrect? this is otherwise a bit confusing for users
There was a problem hiding this comment.
Done in 167820b. I added a comment explaining why this suggestion remains MaybeIncorrect.
| && let def::Res::PrimTy(prim_ty) = ty_res | ||
| && matches!(prim_ty, PrimTy::Int(_) | PrimTy::Uint(_)) | ||
| && let Some(integer_ty) = get_integer_ty(cx, ty, ty_qpath) | ||
| && (!integer_ty.is_nonzero() || self.msrv.meets(cx, msrvs::NONZERO_FROM_STR_RADIX)) |
There was a problem hiding this comment.
How about we inline this? The negation made me do a bit of an double take.
| && (!integer_ty.is_nonzero() || self.msrv.meets(cx, msrvs::NONZERO_FROM_STR_RADIX)) | |
| && (matches!(integer_ty, IntegerTy::Primitive(_)) || self.msrv.meets(cx, msrvs::NONZERO_FROM_STR_RADIX)) |
There was a problem hiding this comment.
Done in 8084d7a. I inlined the MSRV check as suggested.
e16f56a to
8084d7a
Compare
|
Thanks for the review. I addressed all three comments, and all CI checks are passing. Could you take another look? |
This comment has been minimized.
This comment has been minimized.
| // Preserve aliases and paths in the suggestion. This makes it impossible | ||
| // to guarantee that the replacement compiles in every context. | ||
| snippet_with_context( | ||
| cx, | ||
| ty.span, | ||
| exp.span.ctxt(), | ||
| "<integer>", | ||
| &mut Applicability::MaybeIncorrect, |
There was a problem hiding this comment.
I still don't quite understand what kind of code you are concerned about exactly tbh...
Could you given an code-snippet of what you are concearned about?
The reason I am questioning this is to know why here and not in the IntegerTy::Primitive case too?
There was a problem hiding this comment.
You're right — the comment was misleading. MaybeIncorrect is used for the final suggestion for both primitive and NonZero types; it isn't specific to the source snippet.
The snippet is only used for NonZero so aliases and qualified paths are preserved, while primitives use PrimTy::name_str(). I've changed the snippet lookup to use Applicability::Unspecified and shortened the comment.
There was a problem hiding this comment.
Shortenign the comment to Preserve aliases and paths in the suggestion does not tell me WHY you did this.
WHich aliases and paths?
No, actually tell me which aliases and paths you are preserving.
Give me an syntax example (here on github is fine) so that we have this kind of shared understanding.
Also, why Applicability::Unspecified? I don't really think this makes sense, or?
167820b to
dad311d
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This comment has been minimized.
This comment has been minimized.
ea8af18 to
ad3648c
Compare
There was a problem hiding this comment.
Community Review:
Actually, I think the MaybeIncorrect is a mistake or at least somethign that needs to be better explained why it is this way.
I am specifically asking because this seems to work as expected and prints
mod shadowed {
pub type NonZeroU8 = std::num::NonZeroU16;
}
use shadowed::NonZeroU8;
fn similarly_named_type() {
let _ = NonZeroU8::from_str_radix("300", 10);
//~^ from_str_radix_10 -> "300".parse::<NonZeroU8>()
let _ = shadowed::NonZeroU8::from_str_radix("300", 10);
//~^ from_str_radix_10 -> "300".parse::<shadowed::NonZeroU8>()
let _ = std::num::NonZeroU8::from_str_radix("30", 10);
//~^ from_str_radix_10 -> "30".parse::<std::num::NonZeroU8>(
}Where you are semi-right (I don't think you meant this) is that there is an bug that macro generated code is not properly ignored.
So please add the respective guard to the span if it is from an expansion.
That can arguably be a differnt PR though given that the existing lint also does not guard against this, so you don't have to do this if you don't want 😉..
macro_rules! parse_radix_10 {
($t:ty, $s:expr) => {
<$t>::from_str_radix($s, 10)
};
}
fn macro_argument_type() {
let _ = parse_radix_10!(std::num::NonZeroU32, "9");
}generates the follwing:
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
--> tests/ui/from_str_radix_10.rs:134:9
|
LL | <$t>::from_str_radix($s, 10)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `"9".parse::<std::num::NonZeroU32>()`
...
LL | let _ = parse_radix_10!(std::num::NonZeroU32, "9");
| ------------------------------------------ in this macro invocation
|
= note: this error originates in the macro `parse_radix_10` (in Nightly builds, run with -Z macro-backtrace for more info)
ad3648c to
dca676a
Compare
There was a problem hiding this comment.
Community review: I already aproved above, please see #17715 (comment)
There was a problem hiding this comment.
If you add these test cases:
use std::num::NonZero;
trait FakeFromStr: Sized {
fn from_str_radix(src: &str, radix: u32) -> Result<Self, std::num::ParseIntError>;
}
impl FakeFromStr for char {
fn from_str_radix(src: &str, radix: u32) -> Result<Self, std::num::ParseIntError> {
Ok('a')
}
}
impl FakeFromStr for NonZero<char> {
fn from_str_radix(src: &str, radix: u32) -> Result<Self, std::num::ParseIntError> {
Ok(NonZero::new('a').unwrap())
}
}
fn char_cannot_str_from_radix() -> Result<(), Box<dyn std::error::Error>> {
char::from_str_radix("1", 10)?;
NonZero::<char>::from_str_radix("1", 10)?;
}The original lint correctly ignores char, but NonZero<char> is incorrectly flagged by the updated lint.
There was a problem hiding this comment.
Good catch. I restricted the NonZero check to cases where its type parameter is an integer, and added this as a regression test. NonZero<char> is no longer linted.
| } | ||
|
|
||
| NonZeroU16::from_str_radix("8", 10)?; |
There was a problem hiding this comment.
All your tests are for type aliases, it'd be nice to also test a NonZero that isn't a type alias. (You'll need to import std::num::NonZero)
| } | |
| NonZeroU16::from_str_radix("8", 10)?; | |
| } | |
| NonZero::<usize>::from_str_radix("16", 10)?; | |
| //~^ from_str_radix_10 | |
| NonZeroU16::from_str_radix("8", 10)?; |
There was a problem hiding this comment.
Added NonZero::<usize>::from_str_radix to the test cases as suggested.
changelog: [`from_str_radix_10`]: support `NonZero` integer types
dca676a to
1204021
Compare
| } | ||
| } | ||
|
|
||
| fn issue_17715() -> Result<(), Box<dyn std::error::Error>> { |
There was a problem hiding this comment.
nit: I'm not sure if we would usually use the issue_# naming convention for something brought up in review? It is probably fine since issues and pull requests can't have matching numbers. Closest I found was a small precedence to leave a comment linking to the relevant review (done for #9136 (review) and #8737 (review)).
It doesn't really change whether the tests work or not, so I'd defer to whatever t-clippy reviewer gets assigned to the PR.
|
r? @llogiq rustbot has assigned @llogiq for the project review. Use Why was this reviewer chosen?The reviewer was selected based on:
|
closes #17712
The
from_str_radix_10lint now coversNonZero*types, which gainedfrom_str_radixin Rust 1.98.Uses
is_diag_item(sym::NonZero)for detection and guards the suggestion with MSRV 1.98.changelog: [
from_str_radix_10]: now lints onNonZero*types