-
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
Fix some clippy warnings in libsyntax #41957
Conversation
This is mostly removing stray ampersands, needless returns and lifetimes.
r? @pnkfelix (rust_highfive has picked a reviewer for you, use r? to override) |
src/libsyntax/ext/base.rs
Outdated
@@ -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. |
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.
The quote should include ->
as well, probably `backtrace() -> expn_info`
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.
Ah, good catch!
src/libsyntax/ext/tt/macro_parser.rs
Outdated
@@ -39,40 +39,40 @@ | |||
//! Example: Start parsing `a a a a b` against [· a $( a )* a b]. |
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.
Missed `…`
here.
The current rendering of this doc comment is totally broken. Perhaps better just ```text … ```
the whole thing.
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.
Good idea, will update.
src/libsyntax/ext/tt/macro_parser.rs
Outdated
return token::NtTT(p.parse_token_tree()); | ||
} | ||
_ => {} | ||
if let "tt" = name { |
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.
why not if name == "tt"
?
(cc Manishearth/rust-clippy#1716)
src/libsyntax/feature_gate.rs
Outdated
use marker types for now"); | ||
}, | ||
_ => {} | ||
if let ast::ImplPolarity::Negative = polarity { |
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.
if polarity == ast::ImplPolarity::Negative {
?
src/libsyntax/feature_gate.rs
Outdated
}; | ||
if let ast::TyKind::Never = output_ty.node { | ||
return | ||
} | ||
self.visit_ty(output_ty) |
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.
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![]) |
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.
.unwrap_or_else(Vec::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.
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.
@kennytm No, Vec::new()
takes no arguments. @petrochenkov I just answered there.
src/libsyntax/parse/classify.rs
Outdated
/// 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, |
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.
Could move the ast::StmtKind::Item(_)
above here.
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.
Yeah, I usually avoid this due to the fact that it can mess with perf. But in this case it's probably fine.
src/libsyntax/parse/mod.rs
Outdated
@@ -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 |
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 comment can be removed now? 😊 (#8372 is closed by the introduction of while let
)
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.
Ok, I'll just go and remove it.
src/libsyntax/parse/parser.rs
Outdated
self.span_err(neg_span, "inherent implementation can't be negated"); | ||
}, | ||
_ => {} | ||
if let ast::ImplPolarity::Negative = polarity { |
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.
if polarity == ast::ImplPolarity::Negative {
src/libsyntax/print/pprust.rs
Outdated
word(&mut self.s, "!")?; | ||
}, | ||
_ => {} | ||
if let ast::ImplPolarity::Negative = polarity { |
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.
ditto...
src/libsyntax/ext/tt/macro_rules.rs
Outdated
@@ -861,6 +858,6 @@ fn quoted_tt_to_string(tt: "ed::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"), |
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.
make tidy
yells here, 101 chars 😑. Needs to break the string into two lines.
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.
Ok then.
LitKind::Bool(value) => Token::Ident(Ident::with_empty_ctxt(Symbol::intern(if value { | ||
"true" | ||
} else { | ||
"false" |
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.
Style bikeshed: unless if cond { x } else { y }
fits into one line, match
is terser and more readable.
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.
Terser, yes. More readable, I guess we'll have to agree to disagree here.
src/libsyntax/diagnostics/plugin.rs
Outdated
@@ -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') { |
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.
Why does clippy recommend this replacement?
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.
@petrochenkov I don't see why not?
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.
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.
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.
@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.
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'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()); |
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.
Why does clippy recommend this replacement?
It thinks keywords::Invalid.ident()
may be "heavy"? (It's not actually, and unwrap_or
looked better.)
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.
@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.
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.
@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)) |
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.
Style bikeshed: Closures, closures... Nobody likes good old obvious for loops anymore :(
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 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); |
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.
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.
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)] = &[ |
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.
Nice.
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.
Yeah, clippy warned about the complex type, but I just figured I make use of my RFC 1623 (default static lifetime in statics).
src/libsyntax/parse/parser.rs
Outdated
token::BinOp(token::And) => 1, | ||
token::AndAnd => 1, | ||
token::BinOp(token::And) | | ||
token::AndAnd | | ||
_ if self.token.is_keyword(keywords::Mut) => 1, |
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.
Wait. This changes semantics.
if
clause should apply only to _
, not BinOp and AndAnd.
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.
Good catch! Should be fixed now.
src/libsyntax/parse/parser.rs
Outdated
@@ -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()) |
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.
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.
Hmmm...I'm ok with backing this out, but it does sound like a perf bug to me.
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.
Wait a minute. I just benchmarked and found splitting by char be consistently faster on my machine, no matter the assembly.
All my comments except for #41957 (comment) are non-blocking. |
Travis still fails:
|
Ouch, I must have re-introduced this. Fixing right now. |
Looks like Travis is still failing with the tidy error. |
The build fails:
|
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. |
Travis is still failing, due to some changes in
|
That error seems to be related. I'll investigate. |
@bors r+ |
📌 Commit 282b402 has been approved by |
…nkov Fix some clippy warnings in libsyntax 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. Basically a lot of small changes.