Skip to content

Commit ee5a212

Browse files
author
Ayush Kumar Mishra
committed
Refactoring and added test-cases #70528
1 parent 0315864 commit ee5a212

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Diff for: src/librustc_lexer/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,17 @@ pub enum Base {
236236
/// (e.g. "#![deny(missing_docs)]").
237237
pub fn strip_shebang(input: &str) -> Option<usize> {
238238
debug_assert!(!input.is_empty());
239-
if !input.starts_with("#!") || input.starts_with("#![") || input.starts_with("#! [") {
239+
let s: &str = &remove_whitespace(input);
240+
if !s.starts_with("#!") || s.starts_with("#![") || s.starts_with("#! [") {
240241
return None;
241242
}
242243
Some(input.find('\n').unwrap_or(input.len()))
243244
}
244245

246+
fn remove_whitespace(s: &str) -> String {
247+
s.chars().filter(|c| !c.is_whitespace()).collect()
248+
}
249+
245250
/// Parses the first token from the provided input string.
246251
pub fn first_token(input: &str) -> Token {
247252
debug_assert!(!input.is_empty());

Diff for: src/librustc_lexer/src/tests.rs

+19
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,23 @@ mod tests {
145145
}),
146146
);
147147
}
148+
149+
#[test]
150+
fn test_valid_shebang() {
151+
// https://github.com/rust-lang/rust/issues/70528
152+
let input = "#!/usr/bin/rustrun";
153+
let actual = strip_shebang(input);
154+
let expected: Option<usize> = Some(18);
155+
assert_eq!(expected, actual);
156+
}
157+
158+
#[test]
159+
fn test_invalid_shebang_valid_rust_syntax() {
160+
// https://github.com/rust-lang/rust/issues/70528
161+
let input = "#! [bad_attribute]";
162+
let actual = strip_shebang(input);
163+
let expected: Option<usize> = None;
164+
assert_eq!(expected, actual);
165+
}
166+
148167
}

0 commit comments

Comments
 (0)