Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

tesuji
Copy link
Contributor

@tesuji tesuji commented Jul 5, 2020

No description provided.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 5, 2020
@tesuji tesuji force-pushed the strip branch 4 times, most recently from 2657b7f to 35e6ad4 Compare July 5, 2020 06:32
Comment on lines +439 to +442
name if {
suffix = name.strip_prefix("simd_shuffle");
suffix.is_some()
} =>
Copy link
Contributor Author

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.

Copy link
Member

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.

@Manishearth
Copy link
Member

(I usually don't do arbitrary reviews)

@tesuji tesuji force-pushed the strip branch 2 times, most recently from 7f6260f to ee90da5 Compare July 5, 2020 12:59
@tesuji
Copy link
Contributor Author

tesuji commented Jul 5, 2020

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.
Copy link
Member

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)
Copy link
Member

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("*/"))
Copy link
Member

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) => {
Copy link
Member

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")?
Copy link
Member

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());
Copy link
Member

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);
Copy link
Member

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('!')
Copy link
Member

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(';')));
Copy link
Member

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();
Copy link
Member

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.

@tesuji
Copy link
Contributor Author

tesuji commented Jul 5, 2020

Thanks for the review.
Actually I think I will split this to multiple PRs for easier reviewing.

@tesuji tesuji closed this Jul 5, 2020
@Mark-Simulacrum
Copy link
Member

Having already reviewed most of this (modulo clippy), it would be great for those PRs to incorporate existing feedback at least. Thanks!

@tesuji tesuji deleted the strip branch June 8, 2024 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants