@@ -76,8 +76,8 @@ fn check_lit(cx: &EarlyContext<'_>, lit: &Lit, span: Span, is_string: bool) {
76
76
if ch == '\\' {
77
77
if let Some ( ( _, '0' ) ) = iter. next ( ) {
78
78
// collect up to two further octal digits
79
- if let Some ( ( mut to, '0' ..='7' ) ) = iter . next ( ) {
80
- if let Some ( ( _, '0' ..='7' ) ) = iter . peek ( ) {
79
+ if let Some ( ( mut to, _ ) ) = iter . next_if ( | ( _ , ch ) | matches ! ( ch , '0' ..='7' ) ) {
80
+ if iter . next_if ( | ( _, ch ) | matches ! ( ch , '0' ..='7' ) ) . is_some ( ) {
81
81
to += 1 ;
82
82
}
83
83
found. push ( ( from, to + 1 ) ) ;
@@ -90,32 +90,6 @@ fn check_lit(cx: &EarlyContext<'_>, lit: &Lit, span: Span, is_string: bool) {
90
90
return ;
91
91
}
92
92
93
- // construct two suggestion strings, one with \x escapes with octal meaning
94
- // as in C, and one with \x00 for null bytes.
95
- let mut suggest_1 = if is_string { "\" " } else { "b\" " } . to_string ( ) ;
96
- let mut suggest_2 = suggest_1. clone ( ) ;
97
- let mut index = 0 ;
98
- for ( from, to) in found {
99
- suggest_1. push_str ( & contents[ index..from] ) ;
100
- suggest_2. push_str ( & contents[ index..from] ) ;
101
-
102
- // construct a replacement escape
103
- // the maximum value is \077, or \x3f, so u8 is sufficient here
104
- if let Ok ( n) = u8:: from_str_radix ( & contents[ from + 1 ..to] , 8 ) {
105
- write ! ( suggest_1, "\\ x{n:02x}" ) . unwrap ( ) ;
106
- }
107
-
108
- // append the null byte as \x00 and the following digits literally
109
- suggest_2. push_str ( "\\ x00" ) ;
110
- suggest_2. push_str ( & contents[ from + 2 ..to] ) ;
111
-
112
- index = to;
113
- }
114
- suggest_1. push_str ( & contents[ index..] ) ;
115
- suggest_1. push ( '"' ) ;
116
- suggest_2. push_str ( & contents[ index..] ) ;
117
- suggest_2. push ( '"' ) ;
118
-
119
93
span_lint_and_then (
120
94
cx,
121
95
OCTAL_ESCAPES ,
@@ -129,23 +103,53 @@ fn check_lit(cx: &EarlyContext<'_>, lit: &Lit, span: Span, is_string: bool) {
129
103
"octal escapes are not supported, `\\ 0` is always a null {}" ,
130
104
if is_string { "character" } else { "byte" }
131
105
) ) ;
132
- // suggestion 1: equivalent hex escape
133
- diag. span_suggestion (
134
- span,
135
- "if an octal escape was intended, use the hexadecimal representation instead" ,
136
- suggest_1,
137
- Applicability :: MaybeIncorrect ,
138
- ) ;
139
- // suggestion 2: unambiguous null byte
140
- diag. span_suggestion (
141
- span,
142
- format ! (
143
- "if the null {} is intended, disambiguate using" ,
144
- if is_string { "character" } else { "byte" }
145
- ) ,
146
- suggest_2,
147
- Applicability :: MaybeIncorrect ,
148
- ) ;
106
+
107
+ // Generate suggestions if the string is not too long (~ 5 lines)
108
+ if contents. len ( ) < 400 {
109
+ // construct two suggestion strings, one with \x escapes with octal meaning
110
+ // as in C, and one with \x00 for null bytes.
111
+ let mut suggest_1 = if is_string { "\" " } else { "b\" " } . to_string ( ) ;
112
+ let mut suggest_2 = suggest_1. clone ( ) ;
113
+ let mut index = 0 ;
114
+ for ( from, to) in found {
115
+ suggest_1. push_str ( & contents[ index..from] ) ;
116
+ suggest_2. push_str ( & contents[ index..from] ) ;
117
+
118
+ // construct a replacement escape
119
+ // the maximum value is \077, or \x3f, so u8 is sufficient here
120
+ if let Ok ( n) = u8:: from_str_radix ( & contents[ from + 1 ..to] , 8 ) {
121
+ write ! ( suggest_1, "\\ x{n:02x}" ) . unwrap ( ) ;
122
+ }
123
+
124
+ // append the null byte as \x00 and the following digits literally
125
+ suggest_2. push_str ( "\\ x00" ) ;
126
+ suggest_2. push_str ( & contents[ from + 2 ..to] ) ;
127
+
128
+ index = to;
129
+ }
130
+ suggest_1. push_str ( & contents[ index..] ) ;
131
+ suggest_2. push_str ( & contents[ index..] ) ;
132
+
133
+ suggest_1. push ( '"' ) ;
134
+ suggest_2. push ( '"' ) ;
135
+ // suggestion 1: equivalent hex escape
136
+ diag. span_suggestion (
137
+ span,
138
+ "if an octal escape was intended, use the hexadecimal representation instead" ,
139
+ suggest_1,
140
+ Applicability :: MaybeIncorrect ,
141
+ ) ;
142
+ // suggestion 2: unambiguous null byte
143
+ diag. span_suggestion (
144
+ span,
145
+ format ! (
146
+ "if the null {} is intended, disambiguate using" ,
147
+ if is_string { "character" } else { "byte" }
148
+ ) ,
149
+ suggest_2,
150
+ Applicability :: MaybeIncorrect ,
151
+ ) ;
152
+ }
149
153
} ,
150
154
) ;
151
155
}
0 commit comments