use strip_prefix() instead of starts_with and slicing (clippy::manual_strip)

This commit is contained in:
Matthias Krüger 2021-03-21 12:38:21 +01:00
parent 3d9b3a8575
commit 8a67116857
4 changed files with 9 additions and 13 deletions

View file

@ -48,9 +48,8 @@ impl Name {
/// Resolve a name from the text of token. /// Resolve a name from the text of token.
fn resolve(raw_text: &str) -> Name { fn resolve(raw_text: &str) -> Name {
let raw_start = "r#"; if let Some(text) = raw_text.strip_prefix("r#") {
if raw_text.starts_with(raw_start) { Name::new_text(SmolStr::new(text))
Name::new_text(SmolStr::new(&raw_text[raw_start.len()..]))
} else { } else {
Name::new_text(raw_text.into()) Name::new_text(raw_text.into())
} }

View file

@ -333,8 +333,7 @@ impl ast::Use {
.and_then(ast::Whitespace::cast); .and_then(ast::Whitespace::cast);
if let Some(next_ws) = next_ws { if let Some(next_ws) = next_ws {
let ws_text = next_ws.syntax().text(); let ws_text = next_ws.syntax().text();
if ws_text.starts_with('\n') { if let Some(rest) = ws_text.strip_prefix('\n') {
let rest = &ws_text[1..];
if rest.is_empty() { if rest.is_empty() {
res.delete(next_ws.syntax()) res.delete(next_ws.syntax())
} else { } else {

View file

@ -154,8 +154,8 @@ fn hide_hash_comments(text: &str) -> String {
fn reveal_hash_comments(text: &str) -> String { fn reveal_hash_comments(text: &str) -> String {
text.split('\n') // want final newline text.split('\n') // want final newline
.map(|it| { .map(|it| {
if it.starts_with("# ") { if let Some(stripped) = it.strip_prefix("# ") {
&it[2..] stripped
} else if it == "#" { } else if it == "#" {
"" ""
} else { } else {

View file

@ -60,12 +60,10 @@ fn collect_tests(s: &str) -> Vec<Test> {
let mut res = Vec::new(); let mut res = Vec::new();
for comment_block in extract_comment_blocks(s) { for comment_block in extract_comment_blocks(s) {
let first_line = &comment_block[0]; let first_line = &comment_block[0];
let (name, ok) = if first_line.starts_with("test ") { let (name, ok) = if let Some(name) = first_line.strip_prefix("test ") {
let name = first_line["test ".len()..].to_string(); (name.to_string(), true)
(name, true) } else if let Some(name) = first_line.strip_prefix("test_err ") {
} else if first_line.starts_with("test_err ") { (name.to_string(), false)
let name = first_line["test_err ".len()..].to_string();
(name, false)
} else { } else {
continue; continue;
}; };