From a9c163ebe9deeaf74699fc8642d919cdb2b5e617 Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Fri, 12 May 2017 20:05:39 +0200 Subject: Fix some clippy warnings in libsyntax This is mostly removing stray ampersands, needless returns and lifetimes. --- src/libsyntax/parse/parser.rs | 73 +++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 38 deletions(-) (limited to 'src/libsyntax/parse/parser.rs') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index ca1351e3b41..28c57e0855f 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -248,7 +248,7 @@ impl TokenCursor { fn next_desugared(&mut self) -> TokenAndSpan { let (sp, name) = match self.next() { TokenAndSpan { sp, tok: token::DocComment(name) } => (sp, name), - tok @ _ => return tok, + tok => return tok, }; let stripped = strip_doc_comment_decoration(&name.as_str()); @@ -354,7 +354,7 @@ pub enum Error { } impl Error { - pub fn span_err<'a>(self, sp: Span, handler: &'a errors::Handler) -> DiagnosticBuilder<'a> { + pub fn span_err(self, sp: Span, handler: &errors::Handler) -> DiagnosticBuilder { match self { Error::FileNotFoundForModule { ref mod_name, ref default_path, @@ -478,9 +478,10 @@ impl<'a> Parser<'a> { } fn next_tok(&mut self) -> TokenAndSpan { - let mut next = match self.desugar_doc_comments { - true => self.token_cursor.next_desugared(), - false => self.token_cursor.next(), + let mut next = if self.desugar_doc_comments { + self.token_cursor.next_desugared() + } else { + self.token_cursor.next() }; if next.sp == syntax_pos::DUMMY_SP { next.sp = self.prev_span; @@ -551,7 +552,7 @@ impl<'a> Parser<'a> { // This might be a sign we need a connect method on Iterator. let b = i.next() .map_or("".to_string(), |t| t.to_string()); - i.enumerate().fold(b, |mut b, (i, ref a)| { + i.enumerate().fold(b, |mut b, (i, a)| { if tokens.len() > 2 && i == tokens.len() - 2 { b.push_str(", or "); } else if tokens.len() == 2 && i == tokens.len() - 2 { @@ -985,18 +986,15 @@ impl<'a> Parser<'a> { token::CloseDelim(..) | token::Eof => break, _ => {} }; - match sep.sep { - Some(ref t) => { - if first { - first = false; - } else { - if let Err(e) = self.expect(t) { - fe(e); - break; - } + if let Some(ref t) = sep.sep { + if first { + first = false; + } else { + if let Err(e) = self.expect(t) { + fe(e); + break; } } - _ => () } if sep.trailing_sep_allowed && kets.iter().any(|k| self.check(k)) { break; @@ -1493,7 +1491,7 @@ impl<'a> Parser<'a> { let sum_span = ty.span.to(self.prev_span); let mut err = struct_span_err!(self.sess.span_diagnostic, sum_span, E0178, - "expected a path on the left-hand side of `+`, not `{}`", pprust::ty_to_string(&ty)); + "expected a path on the left-hand side of `+`, not `{}`", pprust::ty_to_string(ty)); match ty.node { TyKind::Rptr(ref lifetime, ref mut_ty) => { @@ -1547,8 +1545,8 @@ impl<'a> Parser<'a> { pub fn is_named_argument(&mut self) -> bool { let offset = match self.token { - token::BinOp(token::And) => 1, - token::AndAnd => 1, + token::BinOp(token::And) | + token::AndAnd | _ if self.token.is_keyword(keywords::Mut) => 1, _ => 0 }; @@ -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()) }); err.span_suggestion( lo.to(self.prev_span), @@ -3154,10 +3152,11 @@ impl<'a> Parser<'a> { let attrs = self.parse_outer_attributes()?; let pats = self.parse_pats()?; - let mut guard = None; - if self.eat_keyword(keywords::If) { - guard = Some(self.parse_expr()?); - } + let guard = if self.eat_keyword(keywords::If) { + Some(self.parse_expr()?) + } else { + None + }; self.expect(&token::FatArrow)?; let expr = self.parse_expr_res(RESTRICTION_STMT_EXPR, None)?; @@ -3600,10 +3599,11 @@ impl<'a> Parser<'a> { let lo = self.span; let pat = self.parse_pat()?; - let mut ty = None; - if self.eat(&token::Colon) { - ty = Some(self.parse_ty()?); - } + let ty = if self.eat(&token::Colon) { + Some(self.parse_ty()?) + } else { + None + }; let init = self.parse_initializer()?; Ok(P(ast::Local { ty: ty, @@ -3929,7 +3929,7 @@ impl<'a> Parser<'a> { }, None => { let unused_attrs = |attrs: &[_], s: &mut Self| { - if attrs.len() > 0 { + if !attrs.is_empty() { if s.prev_token_kind == PrevTokenKind::DocComment { s.span_fatal_err(s.prev_span, Error::UselessDocComment).emit(); } else { @@ -4815,7 +4815,7 @@ impl<'a> Parser<'a> { self.expect(&token::Not)?; } - self.complain_if_pub_macro(&vis, prev_span); + self.complain_if_pub_macro(vis, prev_span); // eat a matched-delimiter token tree: *at_end = true; @@ -4917,13 +4917,10 @@ impl<'a> Parser<'a> { } } } else { - match polarity { - ast::ImplPolarity::Negative => { - // This is a negated type implementation - // `impl !MyType {}`, which is not allowed. - self.span_err(neg_span, "inherent implementation can't be negated"); - }, - _ => {} + if let ast::ImplPolarity::Negative = polarity { + // This is a negated type implementation + // `impl !MyType {}`, which is not allowed. + self.span_err(neg_span, "inherent implementation can't be negated"); } None }; @@ -5185,7 +5182,7 @@ impl<'a> Parser<'a> { let path_span = self.prev_span; let help_msg = format!("make this visible only to module `{}` with `in`:", path); self.expect(&token::CloseDelim(token::Paren))?; // `)` - let mut err = self.span_fatal_help(path_span, &msg, &suggestion); + let mut err = self.span_fatal_help(path_span, msg, suggestion); err.span_suggestion(path_span, &help_msg, format!("in {}", path)); err.emit(); // emit diagnostic, but continue with public visibility } -- cgit 1.4.1-3-g733a5 From 958c67d9c8ca74370e2052fd8a3d209b8184d1f4 Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Sat, 13 May 2017 21:40:06 +0200 Subject: adressed comments by @kennytm and @petrochenkov --- src/libsyntax/diagnostics/plugin.rs | 4 +-- src/libsyntax/ext/base.rs | 4 +-- src/libsyntax/ext/tt/macro_parser.rs | 50 +++++++++++++++++++----------------- src/libsyntax/ext/tt/macro_rules.rs | 3 ++- src/libsyntax/feature_gate.rs | 7 +++-- src/libsyntax/parse/classify.rs | 2 +- src/libsyntax/parse/mod.rs | 1 - src/libsyntax/parse/parser.rs | 6 ++--- src/libsyntax/print/pprust.rs | 2 +- 9 files changed, 41 insertions(+), 38 deletions(-) (limited to 'src/libsyntax/parse/parser.rs') diff --git a/src/libsyntax/diagnostics/plugin.rs b/src/libsyntax/diagnostics/plugin.rs index ca89a80fdee..73aeb40df84 100644 --- a/src/libsyntax/diagnostics/plugin.rs +++ b/src/libsyntax/diagnostics/plugin.rs @@ -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") { ecx.span_err(span, &format!( "description for error code {} doesn't start and end with a newline", code @@ -120,7 +120,7 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt, // URLs can be unavoidably longer than the line limit, so we allow them. // Allowed format is: `[name]: https://www.rust-lang.org/` - let is_url = |l: &str| l.starts_with('[') && l.contains("]:") && l.contains("http"); + let is_url = |l: &str| l.starts_with("[") && l.contains("]:") && l.contains("http"); if msg.lines().any(|line| line.len() > MAX_DESCRIPTION_WIDTH && !is_url(line)) { ecx.span_err(span, &format!( diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index f78089aaa75..31a7e0d58d0 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -635,8 +635,8 @@ 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. +/// when a macro expansion occurs, the resulting nodes have the `backtrace() +/// -> expn_info` of their expansion context stored into their span. pub struct ExtCtxt<'a> { pub parse_sess: &'a parse::ParseSess, pub ecfg: expand::ExpansionConfig<'a>, diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 8e28135b11d..a0103a1e3fe 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -36,43 +36,47 @@ //! repetitions indicated by Kleene stars. It only advances or calls out to the //! real Rust parser when no `cur_eis` items remain //! -//! Example: Start parsing `a a a a b` against [· a $( a )* a b]. +//! Example: //! -//! Remaining input: `a a a a b` -//! `next_eis`: `[· a $( a )* a b]` +//! ```text, ignore +//! Start parsing a a a a b against [· a $( a )* a b]. //! -//! - - - Advance over an `a`. - - - +//! Remaining input: a a a a b +//! next_eis: [· a $( a )* a b] //! -//! Remaining input: `a a a b` -//! cur: `[a · $( a )* a b]` +//! - - - Advance over an a. - - - +//! +//! Remaining input: a a a b +//! cur: [a · $( a )* a b] //! Descend/Skip (first item). -//! next: `[a $( · a )* a b] [a $( a )* · a b]`. +//! next: [a $( · a )* a b] [a $( a )* · a b]. //! -//! - - - Advance over an `a`. - - - +//! - - - Advance over an a. - - - //! -//! Remaining input: `a a b` -//! cur: `[a $( a · )* a b]` next: `[a $( a )* a · b]` +//! Remaining input: a a b +//! cur: [a $( a · )* a b] next: [a $( a )* a · b] //! Finish/Repeat (first item) -//! next: `[a $( a )* · a b] [a $( · a )* a b] [a $( a )* a · b]` +//! next: [a $( a )* · a b] [a $( · a )* a b] [a $( a )* a · b] //! -//! - - - Advance over an `a`. - - - (this looks exactly like the last step) +//! - - - Advance over an a. - - - (this looks exactly like the last step) //! -//! Remaining input: `a b` -//! cur: `[a $( a · )* a b]` next: `[a $( a )* a · b]` +//! Remaining input: a b +//! cur: [a $( a · )* a b] next: [a $( a )* a · b] //! Finish/Repeat (first item) -//! next: `[a $( a )* · a b] [a $( · a )* a b] [a $( a )* a · b]` +//! next: [a $( a )* · a b] [a $( · a )* a b] [a $( a )* a · b] //! -//! - - - Advance over an `a`. - - - (this looks exactly like the last step) +//! - - - Advance over an a. - - - (this looks exactly like the last step) //! -//! Remaining input: `b` -//! cur: `[a $( a · )* a b]` next: `[a $( a )* a · b]` +//! Remaining input: b +//! cur: [a $( a · )* a b] next: [a $( a )* a · b] //! Finish/Repeat (first item) -//! next: `[a $( a )* · a b] [a $( · a )* a b]` +//! next: [a $( a )* · a b] [a $( · a )* a b] //! -//! - - - Advance over a `b`. - - - +//! - - - Advance over a b. - - - //! -//! Remaining input: `` -//! eof: `[a $( a )* a b ·]` +//! Remaining input: '' +//! eof: [a $( a )* a b ·] +//! ``` pub use self::NamedMatch::*; pub use self::ParseResult::*; @@ -485,7 +489,7 @@ pub fn parse(sess: &ParseSess, tts: TokenStream, ms: &[TokenTree], directory: Op } fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal { - if let "tt" = name { + if name == "tt" { return token::NtTT(p.parse_token_tree()); } // check at the beginning and the parser checks after each bump diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 9e3fe30e7bf..4a307ab18a5 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -858,6 +858,7 @@ 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"), } } diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index f95693f6820..09090ab8731 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -1216,7 +1216,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } ast::ItemKind::Impl(_, polarity, defaultness, _, _, _, _) => { - if let ast::ImplPolarity::Negative = polarity { + if polarity == ast::ImplPolarity::Negative { gate_feature_post!(&self, optin_builtin_traits, i.span, "negative trait bounds are not yet fully implemented; \ @@ -1269,10 +1269,9 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { fn visit_fn_ret_ty(&mut self, ret_ty: &'a ast::FunctionRetTy) { if let ast::FunctionRetTy::Ty(ref output_ty) = *ret_ty { - if let ast::TyKind::Never = output_ty.node { - return + if output_ty.node != ast::TyKind::Never { + self.visit_ty(output_ty) } - self.visit_ty(output_ty) } } diff --git a/src/libsyntax/parse/classify.rs b/src/libsyntax/parse/classify.rs index c2755cf0591..0c6f09ba766 100644 --- a/src/libsyntax/parse/classify.rs +++ b/src/libsyntax/parse/classify.rs @@ -48,8 +48,8 @@ pub fn expr_is_simple_block(e: &ast::Expr) -> bool { 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::Item(_) | ast::StmtKind::Semi(..) | ast::StmtKind::Mac(..) => false, } diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 4fcf7614622..1eff819d755 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -341,7 +341,6 @@ pub fn raw_str_lit(lit: &str) -> String { debug!("raw_str_lit: given {}", escape_default(lit)); let mut res = String::with_capacity(lit.len()); - // FIXME #8372: This could be a for-loop if it didn't borrow the iterator let mut chars = lit.chars().peekable(); while let Some(c) = chars.next() { if c == '\r' { diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 28c57e0855f..4741f896d3c 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1546,7 +1546,7 @@ impl<'a> Parser<'a> { pub fn is_named_argument(&mut self) -> bool { let offset = match self.token { token::BinOp(token::And) | - token::AndAnd | + token::AndAnd => 1, _ if self.token.is_keyword(keywords::Mut) => 1, _ => 0 }; @@ -2569,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()) }); err.span_suggestion( lo.to(self.prev_span), @@ -4917,7 +4917,7 @@ impl<'a> Parser<'a> { } } } else { - if let ast::ImplPolarity::Negative = polarity { + if polarity == ast::ImplPolarity::Negative { // This is a negated type implementation // `impl !MyType {}`, which is not allowed. self.span_err(neg_span, "inherent implementation can't be negated"); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 6114db25fe8..bdb4ce34b91 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1327,7 +1327,7 @@ impl<'a> State<'a> { space(&mut self.s)?; } - if let ast::ImplPolarity::Negative = polarity { + if polarity == ast::ImplPolarity::Negative { word(&mut self.s, "!")?; } -- cgit 1.4.1-3-g733a5