Skip to content

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

Merged
merged 2 commits into from
Nov 28, 2024
Merged

Conversation

OshinoShinobu-Chan
Copy link
Contributor

This PR is to fix issue #6821.

I add parse_octal_up_to_three_digits_with_warning which is similar to parse_octal_up_to_three_digits but can print warning. Becuase parse_octal_up_to_three_digits is 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.

Copy link

GNU testsuite comparison:

Skip an intermittent issue tests/tail/inotify-dir-recreate (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/timeout/timeout (fails in this run but passes in the 'main' branch)

Comment on lines 332 to 335
match u8::from_str_radix(str_to_parse, 8) {
Ok(ue) => Some(ue),
Err(_pa) => None,
}
Copy link
Contributor

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:

Suggested change
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()

Copy link
Contributor

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?!

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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 :)

Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map_opt(
recognize(many_m_n(1, 3, one_of("01234567"))),
|out: &[u8]| {
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) => {
Copy link
Contributor

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(_):

Suggested change
Err(_pa) => {
Err(_) => {

Copy link
Contributor Author

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.

Comment on lines 345 to 359
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
}
}
Copy link
Contributor

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
     );
}

result

This way you could remove one indentation level.

Copy link
Contributor Author

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.

@cakebaker cakebaker merged commit 75de5a0 into uutils:main Nov 28, 2024
62 checks passed
@cakebaker
Copy link
Contributor

@OshinoShinobu-Chan Thanks for your PR :)

github-merge-queue bot pushed a commit to rust-lang/rust-clippy that referenced this pull request Jan 10, 2025
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tr: missing ambiguous octal escape warning
4 participants