]> git.lizzy.rs Git - rust.git/commitdiff
Moar linting: needless_borrow, let_unit_value, ...
authorYuri Astrakhan <YuriAstrakhan@gmail.com>
Sat, 24 Dec 2022 21:09:08 +0000 (16:09 -0500)
committerYuri Astrakhan <YuriAstrakhan@gmail.com>
Sun, 25 Dec 2022 10:07:47 +0000 (05:07 -0500)
* There are a few needless borrows that don't seem to be needed. I even did a quick assembly comparison and posted a q to stackoveflow on it. See [here](https://stackoverflow.com/questions/74910196/advantages-of-pass-by-ref-val-with-impl-intoiteratoritem-impl-asrefstr)
* removed several `let _ = ...` when they don't look necessary (even a few ones that were not suggested by clippy (?))
* there were a few `then(|| ctor{})` that clippy suggested to replace with `then_some(ctor{})` -- seems reasonable?
* some unneeded assignment+return - keep the code a bit leaner
* a few `writeln!` instead of `write!`, or even consolidate write!
* a nice optimization to use `ch.is_ascii_digit` instead of `ch.is_digit(10)`

15 files changed:
crates/flycheck/src/lib.rs
crates/parser/src/grammar.rs
crates/parser/src/shortcuts.rs
crates/parser/src/tests.rs
crates/proc-macro-api/src/process.rs
crates/proc-macro-test/build.rs
crates/syntax/src/ast/prec.rs
crates/syntax/src/ptr.rs
crates/syntax/src/validation.rs
crates/test-utils/src/bench_fixture.rs
crates/test-utils/src/lib.rs
crates/toolchain/src/lib.rs
xtask/src/install.rs
xtask/src/release.rs
xtask/src/release/changelog.rs

index b3e7443d1c1c093b2f67805d7b9fb17baa112771..8605379620ba8163a9e9eb5d06ce5a8af377d494 100644 (file)
@@ -297,11 +297,11 @@ fn check_command(&self) -> Command {
                 let mut cmd = Command::new(toolchain::cargo());
                 cmd.arg(command);
                 cmd.current_dir(&self.root);
-                cmd.args(&["--workspace", "--message-format=json", "--manifest-path"])
+                cmd.args(["--workspace", "--message-format=json", "--manifest-path"])
                     .arg(self.root.join("Cargo.toml").as_os_str());
 
                 for target in target_triples {
-                    cmd.args(&["--target", target.as_str()]);
+                    cmd.args(["--target", target.as_str()]);
                 }
                 if *all_targets {
                     cmd.arg("--all-targets");
index b7468329610a7e247985e32622b6eeb84d994e87..485b612f08187f33695008c1a0ec6733a9edfe72 100644 (file)
@@ -51,7 +51,7 @@ pub(crate) mod prefix {
         use super::*;
 
         pub(crate) fn vis(p: &mut Parser<'_>) {
-            let _ = opt_visibility(p, false);
+            opt_visibility(p, false);
         }
 
         pub(crate) fn block(p: &mut Parser<'_>) {
@@ -70,10 +70,10 @@ pub(crate) fn ty(p: &mut Parser<'_>) {
             types::type_(p);
         }
         pub(crate) fn expr(p: &mut Parser<'_>) {
-            let _ = expressions::expr(p);
+            expressions::expr(p);
         }
         pub(crate) fn path(p: &mut Parser<'_>) {
-            let _ = paths::type_path(p);
+            paths::type_path(p);
         }
         pub(crate) fn item(p: &mut Parser<'_>) {
             items::item_or_macro(p, true);
index 4b805faddcba9fd4b67f0370dbae10ff8cba85bb..2be4050d135793706ab54794561a5225eb8f7791 100644 (file)
@@ -80,8 +80,8 @@ pub fn intersperse_trivia(
             State::PendingEnter | State::Normal => unreachable!(),
         }
 
-        let is_eof = builder.pos == builder.lexed.len();
-        is_eof
+        // is_eof?
+        builder.pos == builder.lexed.len()
     }
 }
 
index caf1a3e83cb934075bcd23006934423ae4fc9b0d..c1b4e9a7d8aec690311f1715e2642c8664d1d585 100644 (file)
@@ -93,14 +93,12 @@ fn parse(entry: TopEntryPoint, text: &str) -> (String, bool) {
         crate::StrStep::Token { kind, text } => {
             assert!(depth > 0);
             len += text.len();
-            write!(buf, "{indent}").unwrap();
-            write!(buf, "{kind:?} {text:?}\n").unwrap();
+            writeln!(buf, "{indent}{kind:?} {text:?}").unwrap();
         }
         crate::StrStep::Enter { kind } => {
             assert!(depth > 0 || len == 0);
             depth += 1;
-            write!(buf, "{indent}").unwrap();
-            write!(buf, "{kind:?}\n").unwrap();
+            writeln!(buf, "{indent}{kind:?}").unwrap();
             indent.push_str("  ");
         }
         crate::StrStep::Exit => {
index c4018d3b39e7705fabcdc36424a9de5a2d37aae3..54dcb17f4e8b009cab7ab15a8fa72752562feeed 100644 (file)
@@ -67,7 +67,7 @@ fn run(
         args: impl IntoIterator<Item = impl AsRef<OsStr>>,
     ) -> io::Result<Process> {
         let args: Vec<OsString> = args.into_iter().map(|s| s.as_ref().into()).collect();
-        let child = JodChild(mk_child(&path, &args)?);
+        let child = JodChild(mk_child(&path, args)?);
         Ok(Process { child })
     }
 
index 340e9f93ed6c26d8eaa09bc9e944b7b199124597..19a5caa4ccda623aacfc5ec5710f049e4a17db7d 100644 (file)
@@ -63,7 +63,7 @@ fn main() {
     };
 
     cmd.current_dir(&staging_dir)
-        .args(&["build", "-p", "proc-macro-test-impl", "--message-format", "json"])
+        .args(["build", "-p", "proc-macro-test-impl", "--message-format", "json"])
         // Explicit override the target directory to avoid using the same one which the parent
         // cargo is using, or we'll deadlock.
         // This can happen when `CARGO_TARGET_DIR` is set or global config forces all cargo
index ac7ef45c1dee5776e13788259945f5c3ff19919e..c118ef063593edf93e6fca74409ea2d0007616ee 100644 (file)
@@ -119,7 +119,7 @@ fn needs_parens_in_stmt(&self, stmt: Option<&ast::Stmt>) -> bool {
     fn binding_power(&self) -> (u8, u8) {
         use ast::{ArithOp::*, BinaryOp::*, Expr::*, LogicOp::*};
 
-        let dps = match self {
+        match self {
             // (0, 0)   -- paren-like/nullary
             // (0, N)   -- prefix
             // (N, 0)   -- postfix
@@ -170,9 +170,7 @@ fn binding_power(&self) -> (u8, u8) {
             ArrayExpr(_) | TupleExpr(_) | Literal(_) | PathExpr(_) | ParenExpr(_) | IfExpr(_)
             | WhileExpr(_) | ForExpr(_) | LoopExpr(_) | MatchExpr(_) | BlockExpr(_)
             | RecordExpr(_) | UnderscoreExpr(_) => (0, 0),
-        };
-
-        dps
+        }
     }
 
     fn is_paren_like(&self) -> bool {
index a886972fff96175494cd48e849f9e2370dd4fae4..1d4a89201ae4265a3a40533345bc2dc7a193d364 100644 (file)
@@ -82,7 +82,7 @@ pub fn upcast<M: AstNode>(self) -> AstPtr<M>
 
     /// Like `SyntaxNodePtr::cast` but the trait bounds work out.
     pub fn try_from_raw(raw: SyntaxNodePtr) -> Option<AstPtr<N>> {
-        N::can_cast(raw.kind()).then(|| AstPtr { raw, _ty: PhantomData })
+        N::can_cast(raw.kind()).then_some(AstPtr { raw, _ty: PhantomData })
     }
 }
 
index 1eea2346451dd42038a9edcb5bdfe3cd368bb20c..fb2381110bfe2438bba0d3629dc4f21636f68b86 100644 (file)
@@ -196,7 +196,7 @@ pub(crate) fn validate_block_structure(root: &SyntaxNode) {
 
 fn validate_numeric_name(name_ref: Option<ast::NameRef>, errors: &mut Vec<SyntaxError>) {
     if let Some(int_token) = int_token(name_ref) {
-        if int_token.text().chars().any(|c| !c.is_digit(10)) {
+        if int_token.text().chars().any(|c| !c.is_ascii_digit()) {
             errors.push(SyntaxError::new(
                 "Tuple (struct) field access is only allowed through \
                 decimal integers with no underscores or suffix",
index 979156263de1b63fd8e2573db1fda9f184338619..9296fd2e683534f502a9d065f97a1d24061cd790 100644 (file)
@@ -36,10 +36,10 @@ struct S{} {{
 
 pub fn glorious_old_parser() -> String {
     let path = project_root().join("bench_data/glorious_old_parser");
-    fs::read_to_string(&path).unwrap()
+    fs::read_to_string(path).unwrap()
 }
 
 pub fn numerous_macro_rules() -> String {
     let path = project_root().join("bench_data/numerous_macro_rules");
-    fs::read_to_string(&path).unwrap()
+    fs::read_to_string(path).unwrap()
 }
index 74468ea750d93f8d55885b91e6282853e9082ecf..ec3bf21405114c90e94536cf46549b287f4c8091 100644 (file)
@@ -396,7 +396,7 @@ pub fn skip_slow_tests() -> bool {
         eprintln!("ignoring slow test");
     } else {
         let path = project_root().join("./target/.slow_tests_cookie");
-        fs::write(&path, ".").unwrap();
+        fs::write(path, ".").unwrap();
     }
     should_skip
 }
index b05da769161e65a020f832f377efe26731b74bb3..67bdad2aadd83450d4eafd64f939f7311487fd82 100644 (file)
@@ -35,7 +35,7 @@ fn get_path_for_executable(executable_name: &'static str) -> PathBuf {
     //      example: for cargo, this tries ~/.cargo/bin/cargo
     //      It seems that this is a reasonable place to try for cargo, rustc, and rustup
     let env_var = executable_name.to_ascii_uppercase();
-    if let Some(path) = env::var_os(&env_var) {
+    if let Some(path) = env::var_os(env_var) {
         return path.into();
     }
 
index ae978d5512e7b8f164b8fb42886e15458e58650f..83223a551d130d607b8d975e10f0ffafe367dbae 100644 (file)
@@ -62,7 +62,7 @@ fn fix_path_for_mac(sh: &Shell) -> Result<()> {
         let mut paths = env::split_paths(&vars).collect::<Vec<_>>();
         paths.append(&mut vscode_path);
         let new_paths = env::join_paths(paths).context("build env PATH")?;
-        sh.set_var("PATH", &new_paths);
+        sh.set_var("PATH", new_paths);
     }
 
     Ok(())
index bfbe95569641360285a66b193a89ee04eb27e226..4a30691477857148bb9437edc795bb83dede188e 100644 (file)
@@ -65,7 +65,7 @@ pub(crate) fn run(self, sh: &Shell) -> anyhow::Result<()> {
 
         let contents = changelog::get_changelog(sh, changelog_n, &commit, prev_tag, &today)?;
         let path = changelog_dir.join(format!("{today}-changelog-{changelog_n}.adoc"));
-        sh.write_file(&path, &contents)?;
+        sh.write_file(path, contents)?;
 
         Ok(())
     }
index 4a06bb9ac081d4b0769a3032f3e9cb024e54f06c..90095df99e89726456cc4c02f0e0a12d7ec275e1 100644 (file)
@@ -23,7 +23,7 @@ pub(crate) fn get_changelog(
     let mut others = String::new();
     for line in git_log.lines() {
         let line = line.trim_start();
-        if let Some(pr_num) = parse_pr_number(&line) {
+        if let Some(pr_num) = parse_pr_number(line) {
             let accept = "Accept: application/vnd.github.v3+json";
             let authorization = format!("Authorization: token {token}");
             let pr_url = "https://api.github.com/repos/rust-lang/rust-analyzer/issues";