- 
          
- 
                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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|  | @@ -16,13 +16,15 @@ use nom::{ | |||||
| IResult, | ||||||
| }; | ||||||
| use std::{ | ||||||
| char, | ||||||
| collections::{HashMap, HashSet}, | ||||||
| error::Error, | ||||||
| fmt::{Debug, Display}, | ||||||
| io::{BufRead, Write}, | ||||||
| ops::Not, | ||||||
| }; | ||||||
| use uucore::error::UError; | ||||||
| use uucore::show_warning; | ||||||
|  | ||||||
| #[derive(Debug, Clone)] | ||||||
| pub enum BadSequence { | ||||||
|  | @@ -293,7 +295,9 @@ impl Sequence { | |||||
| Self::parse_class, | ||||||
| Self::parse_char_equal, | ||||||
| // NOTE: This must be the last one | ||||||
| map(Self::parse_backslash_or_char, |s| Ok(Self::Char(s))), | ||||||
| map(Self::parse_backslash_or_char_with_warning, |s| { | ||||||
| Ok(Self::Char(s)) | ||||||
| }), | ||||||
| )))(input) | ||||||
| .map(|(_, r)| r) | ||||||
| .unwrap() | ||||||
|  | @@ -302,10 +306,16 @@ impl Sequence { | |||||
| } | ||||||
|  | ||||||
| fn parse_octal(input: &[u8]) -> IResult<&[u8], u8> { | ||||||
| // For `parse_char_range`, `parse_char_star`, `parse_char_repeat`, `parse_char_equal`. | ||||||
| // Because in these patterns, there's no ambiguous cases. | ||||||
| preceded(tag("\\"), Self::parse_octal_up_to_three_digits)(input) | ||||||
| } | ||||||
|  | ||||||
| fn parse_octal_with_warning(input: &[u8]) -> IResult<&[u8], u8> { | ||||||
| preceded( | ||||||
| tag("\\"), | ||||||
| alt(( | ||||||
| Self::parse_octal_up_to_three_digits, | ||||||
| Self::parse_octal_up_to_three_digits_with_warning, | ||||||
| // Fallback for if the three digit octal escape is greater than \377 (0xFF), and therefore can't be | ||||||
| // parsed as as a byte | ||||||
| // See test `test_multibyte_octal_sequence` | ||||||
|  | @@ -319,13 +329,31 @@ impl Sequence { | |||||
| 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) => None, | ||||||
| } | ||||||
| }, | ||||||
| )(input) | ||||||
| } | ||||||
|  | ||||||
| fn parse_octal_up_to_three_digits_with_warning(input: &[u8]) -> IResult<&[u8], u8> { | ||||||
| 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) => { | ||||||
|          | ||||||
| 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.
        
          
              
                Outdated
          
        
      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.
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 theResultinto anOption: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.
See rust-lang/rust-clippy#13740