-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
tr: Add ambiguous octal escape warning #6886
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
Conversation
e801364 to
30a4724
Compare
|
GNU testsuite comparison: |
30a4724 to
6954ad9
Compare
src/uu/tr/src/operation.rs
Outdated
| match u8::from_str_radix(str_to_parse, 8) { | ||
| Ok(ue) => Some(ue), | ||
| Err(_pa) => None, | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can simplify this and use ok() to turn the Result into an Option:
| match u8::from_str_radix(str_to_parse, 8) { | |
| Ok(ue) => Some(ue), | |
| Err(_pa) => None, | |
| } | |
| u8::from_str_radix(str_to_parse, 8).ok() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i wonder if there is a clippy warning for this?!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sylvestre I'm not aware of such a lint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are right. I just copied it and removed the previous TODO comment, so I didn't notice that this match is redundant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@samueltardieu you might be interested by this :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, this is lacking. I'll add this to my TODO list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/uu/tr/src/operation.rs
Outdated
| let str_to_parse = std::str::from_utf8(out).unwrap(); | ||
| match u8::from_str_radix(str_to_parse, 8) { | ||
| Ok(ue) => Some(ue), | ||
| Err(_pa) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is cleaner to simply use Err(_):
| Err(_pa) => { | |
| Err(_) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The similar reason to the one above, and I agree with this change too. Thank you for your review and suggestions.
src/uu/tr/src/operation.rs
Outdated
| match u8::from_str_radix(str_to_parse, 8) { | ||
| Ok(ue) => Some(ue), | ||
| Err(_pa) => { | ||
| // TODO | ||
| // A warning needs to be printed here | ||
| // See https://github.com/uutils/coreutils/issues/6821 | ||
| let origin_octal: &str = std::str::from_utf8(input).unwrap(); | ||
| let actual_octal_tail: &str = std::str::from_utf8(&input[0..2]).unwrap(); | ||
| let outstand_char: char = char::from_u32(input[2] as u32).unwrap(); | ||
| show_warning!( | ||
| "the ambiguous octal escape \\{} is being\n interpreted as the 2-byte sequence \\0{}, {}", | ||
| origin_octal, | ||
| actual_octal_tail, | ||
| outstand_char | ||
| ); | ||
| None | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here it might be an option to use something like:
let result = u8::from_str_radix(str_to_parse, 8).ok();
if result.is_none() {
let origin_octal: &str = std::str::from_utf8(input).unwrap();
let actual_octal_tail: &str = std::str::from_utf8(&input[0..2]).unwrap();
let outstand_char: char = char::from_u32(input[2] as u32).unwrap();
show_warning!(
"the ambiguous octal escape \\{} is being\n interpreted as the 2-byte sequence \\0{}, {}",
origin_octal,
actual_octal_tail,
outstand_char
);
}
resultThis way you could remove one indentation level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is necessary. But it does make the code looks nicer. I think I'll accept this suggestion.
|
@OshinoShinobu-Chan Thanks for your PR :) |
changelog: [`manual_ok_err`]: new lint
Detect manual implementations of `.ok()` or `.err()`, as in
```rust
let a = match func() {
Ok(v) => Some(v),
Err(_) => None,
};
let b = if let Err(v) = func() {
Some(v)
} else {
None
};
```
which can be replaced by
```rust
let a = func().ok();
let b = func().err();
```
This pattern was detected in the wild in the Rust reimplementation of
coreutils:
uutils/coreutils#6886 (review)
This PR is to fix issue #6821.
I add
parse_octal_up_to_three_digits_with_warningwhich is similar toparse_octal_up_to_three_digitsbut can print warning. Becuaseparse_octal_up_to_three_digitsis alse used in other cases, and those cases should not output the warning. With two versions, we can avoid printing the same warning more than once.Some of the other functions are also added "with_warning" version.