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

Fix some clippy warnings in libsyntax #41957

Merged
merged 3 commits into from
May 17, 2017
Merged

Conversation

llogiq
Copy link
Contributor

@llogiq llogiq commented May 12, 2017

This is mostly removing stray ampersands, needless returns and lifetimes. Basically a lot of small changes.

This is mostly removing stray ampersands, needless returns and lifetimes.
@rust-highfive
Copy link
Collaborator

r? @pnkfelix

(rust_highfive has picked a reviewer for you, use r? to override)

@@ -636,7 +636,7 @@ pub struct ExpansionData {

/// One of these is made during expansion and incrementally updated as we go;
/// when a macro expansion occurs, the resulting nodes have the backtrace()
/// -> expn_info of their expansion context stored into their span.
/// -> `expn_info` of their expansion context stored into their span.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The quote should include -> as well, probably `backtrace() -> expn_info`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good catch!

@@ -39,40 +39,40 @@
//! Example: Start parsing `a a a a b` against [· a $( a )* a b].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed `…` here.

The current rendering of this doc comment is totally broken. Perhaps better just ```text … ``` the whole thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, will update.

return token::NtTT(p.parse_token_tree());
}
_ => {}
if let "tt" = name {
Copy link
Member

@kennytm kennytm May 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not if name == "tt" ?

(cc Manishearth/rust-clippy#1716)

use marker types for now");
},
_ => {}
if let ast::ImplPolarity::Negative = polarity {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if polarity == ast::ImplPolarity::Negative { ?

};
if let ast::TyKind::Never = output_ty.node {
return
}
self.visit_ty(output_ty)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if output_ty.node != ast::TyKind::Never {
    self.visit_ty(output_ty);
}

?

@@ -330,7 +330,7 @@ impl DiagnosticSpanLine {
})
.collect()
})
.unwrap_or(vec![])
.unwrap_or_else(|_| vec![])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.unwrap_or_else(Vec::new)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kennytm No, Vec::new() takes no arguments. @petrochenkov I just answered there.

/// seen the semicolon, and thus don't need another.
pub fn stmt_ends_with_semi(stmt: &ast::StmtKind) -> bool {
match *stmt {
ast::StmtKind::Local(_) => true,
ast::StmtKind::Item(_) => false,
ast::StmtKind::Expr(ref e) => expr_requires_semi_to_be_stmt(e),
ast::StmtKind::Semi(..) => false,
ast::StmtKind::Semi(..) |
ast::StmtKind::Mac(..) => false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could move the ast::StmtKind::Item(_) above here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I usually avoid this due to the fact that it can mess with perf. But in this case it's probably fine.

@@ -348,20 +343,15 @@ pub fn raw_str_lit(lit: &str) -> String {

// FIXME #8372: This could be a for-loop if it didn't borrow the iterator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment can be removed now? 😊 (#8372 is closed by the introduction of while let)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll just go and remove it.

self.span_err(neg_span, "inherent implementation can't be negated");
},
_ => {}
if let ast::ImplPolarity::Negative = polarity {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if polarity == ast::ImplPolarity::Negative {

word(&mut self.s, "!")?;
},
_ => {}
if let ast::ImplPolarity::Negative = polarity {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto...

@@ -861,6 +858,6 @@ fn quoted_tt_to_string(tt: &quoted::TokenTree) -> String {
match *tt {
quoted::TokenTree::Token(_, ref tok) => ::print::pprust::token_to_string(tok),
quoted::TokenTree::MetaVarDecl(_, name, kind) => format!("${}:{}", name, kind),
_ => panic!("unexpected quoted::TokenTree::{Sequence or Delimited} in follow set checker"),
_ => panic!("unexpected quoted::TokenTree::{{Sequence or Delimited}} in follow set checker"),
Copy link
Member

@kennytm kennytm May 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make tidy yells here, 101 chars 😑. Needs to break the string into two lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok then.

LitKind::Bool(value) => Token::Ident(Ident::with_empty_ctxt(Symbol::intern(if value {
"true"
} else {
"false"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style bikeshed: unless if cond { x } else { y } fits into one line, match is terser and more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Terser, yes. More readable, I guess we'll have to agree to disagree here.

@@ -111,7 +111,7 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt,
// overflow the maximum line width.
description.map(|raw_msg| {
let msg = raw_msg.as_str();
if !msg.starts_with("\n") || !msg.ends_with("\n") {
if !msg.starts_with('\n') || !msg.ends_with('\n') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does clippy recommend this replacement?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@petrochenkov I don't see why not?

Copy link
Contributor

@petrochenkov petrochenkov May 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, in theory string search effectively works on byte arrays so "X" is already what it wants, and 'X' is a unicode character and needs to be converted into bytes first, and why would you want to deal with unicode characters at all if you just want to search for a substring.
Something like that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@petrochenkov Ah I see. Indeed msg.starts_with('\n') generates more code. However, this should be something that rustc and/or LLVM that can be optimized out? The input is a constant ASCII character which all UTF-8-related branches should be elided.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with backing this out, but it sounds like a perf bug to me.

@@ -454,7 +454,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
};
let path = &mac.node.path;

let ident = ident.unwrap_or(keywords::Invalid.ident());
let ident = ident.unwrap_or_else(|| keywords::Invalid.ident());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does clippy recommend this replacement?
It thinks keywords::Invalid.ident() may be "heavy"? (It's not actually, and unwrap_or looked better.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@petrochenkov I don't think clippy is able to determine whether a function is heavy or not. It recommends unwrap_or_else whenever the inner expression is not a variable/primitive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@petrochenkov I thought so, too, until I benchmarked a very simple expression (_.unwrap_or..(vec![])) with / without the else. Interestingly, though there's no allocation, and the result should be basically static, the closure version was a wee bit faster. Not that it matters in this place, so if you like, I can roll it back.

None
attrs.iter()
.position(|a| !attr::is_known(a) && !is_builtin_attr(a))
.map(|i| attrs.remove(i))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style bikeshed: Closures, closures... Nobody likes good old obvious for loops anymore :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think that the code better conveys the meaning now.

@@ -94,7 +94,7 @@ fn generic_extension<'cx>(cx: &'cx mut ExtCtxt,
-> Box<MacResult+'cx> {
if cx.trace_macros() {
let sp = sp.macro_backtrace().last().map(|trace| trace.call_site).unwrap_or(sp);
let mut values: &mut Vec<String> = cx.expansions.entry(sp).or_insert(vec![]);
let mut values: &mut Vec<String> = cx.expansions.entry(sp).or_insert_with(Vec::new);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly.

@@ -816,7 +816,7 @@ pub const BUILTIN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeG
];

// cfg(...)'s that are feature gated
const GATED_CFGS: &'static [(&'static str, &'static str, fn(&Features) -> bool)] = &[
const GATED_CFGS: &[(&str, &str, fn(&Features) -> bool)] = &[
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, clippy warned about the complex type, but I just figured I make use of my RFC 1623 (default static lifetime in statics).

token::BinOp(token::And) => 1,
token::AndAnd => 1,
token::BinOp(token::And) |
token::AndAnd |
_ if self.token.is_keyword(keywords::Mut) => 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait. This changes semantics.
if clause should apply only to _, not BinOp and AndAnd.

Copy link
Contributor Author

@llogiq llogiq May 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Should be fixed now.

@@ -2571,7 +2569,7 @@ impl<'a> Parser<'a> {
s.print_usize(float.trunc() as usize)?;
s.pclose()?;
word(&mut s.s, ".")?;
word(&mut s.s, fstr.splitn(2, ".").last().unwrap())
word(&mut s.s, fstr.splitn(2, '.').last().unwrap())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm...I'm ok with backing this out, but it does sound like a perf bug to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait a minute. I just benchmarked and found splitting by char be consistently faster on my machine, no matter the assembly.

@petrochenkov
Copy link
Contributor

All my comments except for #41957 (comment) are non-blocking.

@llogiq llogiq force-pushed the clippy-libsyntax branch from 2b2170d to 232e16f Compare May 13, 2017 20:29
@petrochenkov
Copy link
Contributor

Travis still fails:

tidy error: /checkout/src/libsyntax/ext/tt/macro_parser.rs:40: trailing whitespace

@llogiq
Copy link
Contributor Author

llogiq commented May 13, 2017

Ouch, I must have re-introduced this. Fixing right now.

@Mark-Simulacrum Mark-Simulacrum added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label May 14, 2017
@Mark-Simulacrum
Copy link
Member

Looks like Travis is still failing with the tidy error.

@oli-obk
Copy link
Contributor

oli-obk commented May 15, 2017

The build fails:

[00:02:32]    Compiling syntax v0.0.0 (file:///checkout/src/libsyntax)
[00:02:32] error: unexpected close delimiter: `}`
[00:02:32]     --> /checkout/src/libsyntax/feature_gate.rs:1454:1
[00:02:32]      |
[00:02:32] 1454 | }
[00:02:32]      | ^

@llogiq llogiq force-pushed the clippy-libsyntax branch from 4bb01cc to aefd980 Compare May 15, 2017 10:20
@llogiq llogiq force-pushed the clippy-libsyntax branch from aefd980 to 958c67d Compare May 15, 2017 21:56
@llogiq
Copy link
Contributor Author

llogiq commented May 15, 2017

Sorry, that last push somehow got hung up on network error and completely destroyed my git repo. I took a while to get online (I don't want to clone rust's git repo on a half gig data plan), so now it'll hopefully work.

@petrochenkov
Copy link
Contributor

Travis is still failing, due to some changes in pprust.rs, I suspect.

thread 'main' panicked at 'assertion failed: (left == right) (left: " (ref x, ref y) => (x, y, ),", right: " (ref x, ref y) => (x, y),")', /checkout/src/test/run-pass-fulldeps/qquote.rs:56

@llogiq
Copy link
Contributor Author

llogiq commented May 16, 2017

That error seems to be related. I'll investigate.

@arielb1 arielb1 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 16, 2017
@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented May 16, 2017

📌 Commit 282b402 has been approved by petrochenkov

Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request May 16, 2017
…nkov

Fix some clippy warnings in libsyntax

This is mostly removing stray ampersands, needless returns and lifetimes. Basically a lot of small changes.
bors added a commit that referenced this pull request May 17, 2017
Rollup of 5 pull requests

- Successful merges: #41937, #41957, #42017, #42039, #42046
- Failed merges:
@bors bors merged commit 282b402 into rust-lang:master May 17, 2017
@llogiq llogiq deleted the clippy-libsyntax branch May 18, 2017 18:12
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.

9 participants