-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Use str::strip_{prefix,suffix}
when possible
#74048
Conversation
2657b7f
to
35e6ad4
Compare
name if { | ||
suffix = name.strip_prefix("simd_shuffle"); | ||
suffix.is_some() | ||
} => |
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.
This works around the fact that if-let guard is not implemented (#51114).
At least it gets rid of repeating the pattern in starts_with
with may lead to typos.
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 continue to prefer the old code here.
(I usually don't do arbitrary reviews) |
7f6260f
to
ee90da5
Compare
Oh sorry. I thought you were interested to review this PR. Reassign to random compiler member: r? @petrochenkov |
} | ||
|
||
// FIXME(#64197): Try to privatize this again. |
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.
Seems like this shouldn't be removed? It does look like it could be made private -- only use outside of here seems to be in src/librustc_expand/parse/lexer/tests.rs and those tests could be moved into this crate relatively easily I imagine.
|| s.starts_with("//!") | ||
|| (s.starts_with("/**") && is_block_doc_comment(s)) | ||
|| s.starts_with("/*!") | ||
is_line_doc_comment(s) || is_block_doc_comment(s) |
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.
This seems to be a behavior change. In particular, /*!
returned true before and would now return false, as is_block_doc_comment
only returns true on strings >= 5 characters in length.
match comment | ||
.strip_prefix("/**") | ||
.or_else(|| comment.strip_prefix("/*!")) | ||
.and_then(|s| s.strip_suffix("*/")) |
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.
This also looks like a behavior change -- I don't think we cared what the third character was before, and certainly not the last two characters.
} else { | ||
None | ||
match hi_src.strip_prefix("ref") { | ||
Some(tail) if tail.starts_with(rustc_lexer::is_whitespace) => { |
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.
Pre-existing, but it seems odd to use rustc_lexer::is_whitespace here and char::is_whitespace above. cc @estebank
remnant.is_some() | ||
} => | ||
{ | ||
dl.instruction_address_space = parse_address_space(remnant.unwrap(), "P")? |
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.
w/o if let guards the old code seems better, to be honest.
// Ends with "\\" (it's the case for the last added crate line) | ||
ret.push(line[..line.len() - 1].to_string()); | ||
ret.push(head.to_string()); |
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.
This looks like a change in behavior -- we didn't care if the character was \
before? Probably benign but I'd prefer to not change behavior at all in this patch.
@@ -487,7 +487,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { | |||
} | |||
|
|||
let cx = self.cx; | |||
let dox = item.attrs.collapsed_doc_value().unwrap_or_else(String::new); | |||
let dox = item.collapsed_doc_value().unwrap_or_else(String::new); |
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.
Unrelated change?
link.trim_start_matches("derive@") | ||
} else if link.ends_with('!') { | ||
kind = Some(MacroNS); | ||
link.trim_end_matches('!') |
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.
These change behavior -- trim_start_matches will strip more than once.
// | ||
// We have something like "extern crate foo;", we only care about the "foo". | ||
// Also we don't trim the string cause we believe rustfmt did that for us. | ||
.filter_map(|line| line.strip_prefix("extern crate ").map(|s| s.strip_suffix(';'))); |
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.
This is technically a behavior change -- previously this would work if, say, the character after extern crate is a tab. Regardless I'd personally prefer that the clippy changes go into a PR against the clippy repository rather than here, we should really only be landing changes directly to the subtree if they're "required" (i.e., small patches) to avoid merge conflicts as much as possible.
@@ -1192,8 +1192,7 @@ impl<'test> TestCx<'test> { | |||
for line in reader.lines() { | |||
match line { | |||
Ok(line) => { | |||
let line = | |||
if line.starts_with("//") { line[2..].trim_start() } else { line.as_str() }; | |||
let line = line.strip_prefix("//").unwrap_or(&*line).trim_start(); |
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.
Behavior change: we only trimmed if stripping previously.
Thanks for the review. |
Having already reviewed most of this (modulo clippy), it would be great for those PRs to incorporate existing feedback at least. Thanks! |
No description provided.