From a181fd318b12d0648d126b81ed031d074c8471cf Mon Sep 17 00:00:00 2001 From: Alan Du Date: Mon, 3 Jun 2019 09:43:06 -0400 Subject: Implement cargo lint to run clippy --- .cargo/config | 3 +++ crates/tools/src/lib.rs | 28 ++++++++++++++++++++++++++++ crates/tools/src/main.rs | 4 +++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/.cargo/config b/.cargo/config index b12f407e6..51ae33910 100644 --- a/.cargo/config +++ b/.cargo/config @@ -12,6 +12,9 @@ install-code = "run --package tools --bin tools -- install-code" # Formats the full repository or installs the git hook to do it automatically. format = "run --package tools --bin tools -- format" format-hook = "run --package tools --bin tools -- format-hook" +# Run clippy +lint = "run --package tools --bin tools -- lint" + # Runs the fuzzing test suite (currently only parser) fuzz-tests = "run --package tools --bin tools -- fuzz-tests" diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs index 11b52ccb7..92634655d 100644 --- a/crates/tools/src/lib.rs +++ b/crates/tools/src/lib.rs @@ -133,6 +133,34 @@ pub fn install_format_hook() -> Result<()> { Ok(()) } +pub fn run_clippy() -> Result<()> { + match Command::new("rustup") + .args(&["run", TOOLCHAIN, "--", "cargo", "clippy", "--version"]) + .stderr(Stdio::null()) + .stdout(Stdio::null()) + .status() + { + Ok(status) if status.success() => (), + _ => install_clippy()?, + }; + + let allowed_lints = ["clippy::collapsible_if", "clippy::nonminimal_bool"]; + run( + &format!( + "rustup run {} -- cargo clippy --all-features --all-targets -- -A {}", + TOOLCHAIN, + allowed_lints.join(" -A ") + ), + ".", + )?; + Ok(()) +} + +pub fn install_clippy() -> Result<()> { + run(&format!("rustup install {}", TOOLCHAIN), ".")?; + run(&format!("rustup component add clippy --toolchain {}", TOOLCHAIN), ".") +} + pub fn run_fuzzer() -> Result<()> { match Command::new("cargo") .args(&["fuzz", "--help"]) diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs index 0c3339685..cf189bf1c 100644 --- a/crates/tools/src/main.rs +++ b/crates/tools/src/main.rs @@ -3,7 +3,7 @@ use core::str; use failure::bail; use tools::{ generate, gen_tests, install_format_hook, run, run_with_output, run_rustfmt, - Overwrite, Result, run_fuzzer, + Overwrite, Result, run_fuzzer, run_clippy, }; use std::{path::{PathBuf}, env}; @@ -16,6 +16,7 @@ fn main() -> Result<()> { .subcommand(SubCommand::with_name("format")) .subcommand(SubCommand::with_name("format-hook")) .subcommand(SubCommand::with_name("fuzz-tests")) + .subcommand(SubCommand::with_name("lint")) .get_matches(); match matches.subcommand_name().expect("Subcommand must be specified") { "install-code" => { @@ -28,6 +29,7 @@ fn main() -> Result<()> { "gen-syntax" => generate(Overwrite)?, "format" => run_rustfmt(Overwrite)?, "format-hook" => install_format_hook()?, + "lint" => run_clippy()?, "fuzz-tests" => run_fuzzer()?, _ => unreachable!(), } -- cgit v1.2.3 From 557e90c6ca3c4e754ceeea9c08d8d34590334e77 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Tue, 4 Jun 2019 02:21:29 -0400 Subject: Allow clippy::needless_pass_by_value --- crates/tools/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs index 92634655d..f00e46a34 100644 --- a/crates/tools/src/lib.rs +++ b/crates/tools/src/lib.rs @@ -144,7 +144,8 @@ pub fn run_clippy() -> Result<()> { _ => install_clippy()?, }; - let allowed_lints = ["clippy::collapsible_if", "clippy::nonminimal_bool"]; + let allowed_lints = + ["clippy::collapsible_if", "clippy::nonminimal_bool", "clippy::needless_pass_by_value"]; run( &format!( "rustup run {} -- cargo clippy --all-features --all-targets -- -A {}", -- cgit v1.2.3 From 07ebf5528eec0b054655a11e426e7f0041bcdf82 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Tue, 4 Jun 2019 02:24:47 -0400 Subject: Allow clippy::map_clone We should turn it on after Iterator::copied stabilizes --- crates/tools/src/lib.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs index f00e46a34..b424eb701 100644 --- a/crates/tools/src/lib.rs +++ b/crates/tools/src/lib.rs @@ -144,8 +144,12 @@ pub fn run_clippy() -> Result<()> { _ => install_clippy()?, }; - let allowed_lints = - ["clippy::collapsible_if", "clippy::nonminimal_bool", "clippy::needless_pass_by_value"]; + let allowed_lints = [ + "clippy::collapsible_if", + "clippy::nonminimal_bool", + "clippy::needless_pass_by_value", + "clippy::map_clone", // FIXME: remove when Iterator::copied stabilizes (1.36.0) + ]; run( &format!( "rustup run {} -- cargo clippy --all-features --all-targets -- -A {}", -- cgit v1.2.3 From 573a6bb5c9c61bff42100a8efb576e4ad97425e8 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Mon, 3 Jun 2019 10:00:19 -0400 Subject: Fix clippy::match_bool --- crates/ra_parser/src/grammar.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/ra_parser/src/grammar.rs b/crates/ra_parser/src/grammar.rs index cf603eba1..5997636d6 100644 --- a/crates/ra_parser/src/grammar.rs +++ b/crates/ra_parser/src/grammar.rs @@ -87,10 +87,8 @@ pub(crate) fn pattern(p: &mut Parser) { } pub(crate) fn stmt(p: &mut Parser, with_semi: bool) { - let with_semi = match with_semi { - true => expressions::StmtWithSemi::Yes, - false => expressions::StmtWithSemi::No, - }; + let with_semi = + if with_semi { expressions::StmtWithSemi::Yes } else { expressions::StmtWithSemi::No }; expressions::stmt(p, with_semi) } -- cgit v1.2.3 From 354db651dafd24d93cf0f151d63ad5ecb2e716e2 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Mon, 3 Jun 2019 10:00:51 -0400 Subject: Fix clippy::clone_double_ref --- crates/tools/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs index cf189bf1c..8027ff833 100644 --- a/crates/tools/src/main.rs +++ b/crates/tools/src/main.rs @@ -84,7 +84,7 @@ fn fix_path_for_mac() -> Result<()> { [ROOT_DIR, &home_dir] .iter() - .map(|dir| String::from(dir.clone()) + COMMON_APP_PATH) + .map(|dir| String::from(*dir) + COMMON_APP_PATH) .map(PathBuf::from) .filter(|path| path.exists()) .collect() -- cgit v1.2.3 From ecd420636efe54657ae742ce960ce061740ef108 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Mon, 3 Jun 2019 10:01:10 -0400 Subject: Fix clippy::single_match --- crates/ra_cli/src/analysis_stats.rs | 10 +++--- crates/ra_hir/src/code_model.rs | 5 ++- crates/ra_hir/src/traits.rs | 9 ++---- crates/ra_hir/src/ty/infer.rs | 33 +++++++++----------- crates/ra_hir/src/ty/method_resolution.rs | 38 ++++++++++------------- crates/ra_hir/src/ty/traits/chalk.rs | 9 ++---- crates/ra_ide_api/src/completion/complete_dot.rs | 7 ++--- crates/ra_lsp_server/tests/heavy_tests/support.rs | 7 ++--- crates/thread_worker/src/lib.rs | 9 ++---- 9 files changed, 51 insertions(+), 76 deletions(-) diff --git a/crates/ra_cli/src/analysis_stats.rs b/crates/ra_cli/src/analysis_stats.rs index b481ace9e..8bb524ce3 100644 --- a/crates/ra_cli/src/analysis_stats.rs +++ b/crates/ra_cli/src/analysis_stats.rs @@ -31,18 +31,16 @@ pub fn run(verbose: bool, path: &str, only: Option<&str>) -> Result<()> { for decl in module.declarations(&db) { num_decls += 1; - match decl { - ModuleDef::Function(f) => funcs.push(f), - _ => {} + if let ModuleDef::Function(f) = decl { + funcs.push(f); } } for impl_block in module.impl_blocks(&db) { for item in impl_block.items(&db) { num_decls += 1; - match item { - ImplItem::Method(f) => funcs.push(f), - _ => {} + if let ImplItem::Method(f) = item { + funcs.push(f); } } } diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 9c02b3995..6ee6bd627 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -281,9 +281,8 @@ impl Module { for impl_block in self.impl_blocks(db) { for item in impl_block.items(db) { - match item { - crate::ImplItem::Method(f) => f.diagnostics(db, sink), - _ => (), + if let crate::ImplItem::Method(f) = item { + f.diagnostics(db, sink); } } } diff --git a/crates/ra_hir/src/traits.rs b/crates/ra_hir/src/traits.rs index 2a7c2b791..967654e97 100644 --- a/crates/ra_hir/src/traits.rs +++ b/crates/ra_hir/src/traits.rs @@ -77,13 +77,10 @@ impl TraitItemsIndex { pub(crate) fn trait_items_index(db: &impl DefDatabase, module: Module) -> TraitItemsIndex { let mut index = TraitItemsIndex { traits_by_def: FxHashMap::default() }; for decl in module.declarations(db) { - match decl { - crate::ModuleDef::Trait(tr) => { - for item in tr.trait_data(db).items() { - index.traits_by_def.insert(*item, tr); - } + if let crate::ModuleDef::Trait(tr) = decl { + for item in tr.trait_data(db).items() { + index.traits_by_def.insert(*item, tr); } - _ => {} } } index diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index e8ae33ead..1723921e6 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -848,28 +848,23 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } fn register_obligations_for_call(&mut self, callable_ty: &Ty) { - match callable_ty { - Ty::Apply(a_ty) => match a_ty.ctor { - TypeCtor::FnDef(def) => { - // add obligation for trait implementation, if this is a trait method - // FIXME also register obligations from where clauses from the trait or impl and method - match def { - CallableDef::Function(f) => { - if let Some(trait_) = f.parent_trait(self.db) { - // construct a TraitDef - let substs = a_ty.parameters.prefix( - trait_.generic_params(self.db).count_params_including_parent(), - ); - self.obligations - .push(Obligation::Trait(TraitRef { trait_, substs })); - } + if let Ty::Apply(a_ty) = callable_ty { + if let TypeCtor::FnDef(def) = a_ty.ctor { + // add obligation for trait implementation, if this is a trait method + // FIXME also register obligations from where clauses from the trait or impl and method + match def { + CallableDef::Function(f) => { + if let Some(trait_) = f.parent_trait(self.db) { + // construct a TraitDef + let substs = a_ty.parameters.prefix( + trait_.generic_params(self.db).count_params_including_parent(), + ); + self.obligations.push(Obligation::Trait(TraitRef { trait_, substs })); } - CallableDef::Struct(_) | CallableDef::EnumVariant(_) => {} } + CallableDef::Struct(_) | CallableDef::EnumVariant(_) => {} } - _ => {} - }, - _ => {} + } } } diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 34817a5ec..646e58aa9 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -192,23 +192,20 @@ fn iterate_trait_method_candidates( // iteration let mut known_implemented = false; for item in data.items() { - match item { - &TraitItem::Function(m) => { - let sig = m.signature(db); - if name.map_or(true, |name| sig.name() == name) && sig.has_self_param() { - if !known_implemented { - let trait_ref = canonical_trait_ref(db, t, ty.clone()); - if db.implements(krate, trait_ref).is_none() { - continue 'traits; - } - } - known_implemented = true; - if let Some(result) = callback(&ty.value, m) { - return Some(result); + if let TraitItem::Function(m) = *item { + let sig = m.signature(db); + if name.map_or(true, |name| sig.name() == name) && sig.has_self_param() { + if !known_implemented { + let trait_ref = canonical_trait_ref(db, t, ty.clone()); + if db.implements(krate, trait_ref).is_none() { + continue 'traits; } } + known_implemented = true; + if let Some(result) = callback(&ty.value, m) { + return Some(result); + } } - _ => {} } } } @@ -230,16 +227,13 @@ fn iterate_inherent_methods( for impl_block in impls.lookup_impl_blocks(&ty.value) { for item in impl_block.items(db) { - match item { - ImplItem::Method(f) => { - let sig = f.signature(db); - if name.map_or(true, |name| sig.name() == name) && sig.has_self_param() { - if let Some(result) = callback(&ty.value, f) { - return Some(result); - } + if let ImplItem::Method(f) = item { + let sig = f.signature(db); + if name.map_or(true, |name| sig.name() == name) && sig.has_self_param() { + if let Some(result) = callback(&ty.value, f) { + return Some(result); } } - _ => {} } } } diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs index 78440b258..1e4806db0 100644 --- a/crates/ra_hir/src/ty/traits/chalk.rs +++ b/crates/ra_hir/src/ty/traits/chalk.rs @@ -211,13 +211,10 @@ fn convert_where_clauses( // anyway), otherwise Chalk can easily get into slow situations return vec![pred.clone().subst(substs).to_chalk(db)]; } - match pred { - GenericPredicate::Implemented(trait_ref) => { - if blacklisted_trait(db, trait_ref.trait_) { - continue; - } + if let GenericPredicate::Implemented(trait_ref) = pred { + if blacklisted_trait(db, trait_ref.trait_) { + continue; } - _ => {} } result.push(pred.clone().subst(substs).to_chalk(db)); } diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs index 5bf289c63..0822a0e7e 100644 --- a/crates/ra_ide_api/src/completion/complete_dot.rs +++ b/crates/ra_ide_api/src/completion/complete_dot.rs @@ -16,8 +16,8 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) { for receiver in receiver.autoderef(ctx.db) { - match receiver { - Ty::Apply(a_ty) => match a_ty.ctor { + if let Ty::Apply(a_ty) = receiver { + match a_ty.ctor { TypeCtor::Adt(AdtDef::Struct(s)) => { for field in s.fields(ctx.db) { acc.add_field(ctx, field, &a_ty.parameters); @@ -30,8 +30,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) } } _ => {} - }, - _ => {} + } }; } } diff --git a/crates/ra_lsp_server/tests/heavy_tests/support.rs b/crates/ra_lsp_server/tests/heavy_tests/support.rs index f952a03a3..955d283dd 100644 --- a/crates/ra_lsp_server/tests/heavy_tests/support.rs +++ b/crates/ra_lsp_server/tests/heavy_tests/support.rs @@ -141,15 +141,14 @@ impl Server { R::Params: Serialize, { let actual = self.send_request::(params); - match find_mismatch(&expected_resp, &actual) { - Some((expected_part, actual_part)) => panic!( + if let Some((expected_part, actual_part)) = find_mismatch(&expected_resp, &actual) { + panic!( "JSON mismatch\nExpected:\n{}\nWas:\n{}\nExpected part:\n{}\nActual part:\n{}\n", to_string_pretty(&expected_resp).unwrap(), to_string_pretty(&actual).unwrap(), to_string_pretty(expected_part).unwrap(), to_string_pretty(actual_part).unwrap(), - ), - None => {} + ); } } diff --git a/crates/thread_worker/src/lib.rs b/crates/thread_worker/src/lib.rs index d67e44e38..d8d0d9bf2 100644 --- a/crates/thread_worker/src/lib.rs +++ b/crates/thread_worker/src/lib.rs @@ -19,13 +19,10 @@ impl Drop for ScopedThread { log::info!(".. {} terminated with {}", name, if res.is_ok() { "ok" } else { "err" }); // escalate panic, but avoid aborting the process - match res { - Err(e) => { - if !thread::panicking() { - panic!(e) - } + if let Err(e) = res { + if !thread::panicking() { + panic!(e) } - _ => (), } } } -- cgit v1.2.3 From dddcb0ad940a8010bb5df3d44526729d8e705213 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Mon, 3 Jun 2019 10:01:27 -0400 Subject: Fix clippy::needless_return --- crates/ra_tt/src/buffer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_tt/src/buffer.rs b/crates/ra_tt/src/buffer.rs index 995c9f90b..5659aeae8 100644 --- a/crates/ra_tt/src/buffer.rs +++ b/crates/ra_tt/src/buffer.rs @@ -179,6 +179,6 @@ impl<'a> Cursor<'a> { /// Check whether it is a top level pub fn is_root(&self) -> bool { let entry_id = self.ptr.0; - return entry_id.0 == 0; + entry_id.0 == 0 } } -- cgit v1.2.3 From b9af1d7c428bac3e2efb69e0dadda72938ce3b3c Mon Sep 17 00:00:00 2001 From: Alan Du Date: Mon, 3 Jun 2019 10:07:00 -0400 Subject: Fix clippy::match_ref_pats --- crates/ra_ide_api/src/line_index_utils.rs | 12 ++++++------ crates/ra_text_edit/src/test_utils.rs | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/ra_ide_api/src/line_index_utils.rs b/crates/ra_ide_api/src/line_index_utils.rs index 799a920ad..a03467011 100644 --- a/crates/ra_ide_api/src/line_index_utils.rs +++ b/crates/ra_ide_api/src/line_index_utils.rs @@ -133,9 +133,9 @@ impl<'a> Edits<'a> { } fn next_steps(&mut self, step: &Step) -> NextSteps { - let step_pos = match step { - &Step::Newline(n) => n, - &Step::Utf16Char(r) => r.end(), + let step_pos = match *step { + Step::Newline(n) => n, + Step::Utf16Char(r) => r.end(), }; let res = match &mut self.current { Some(edit) => { @@ -181,9 +181,9 @@ impl<'a> Edits<'a> { if self.acc_diff == 0 { x.clone() } else { - match x { - &Step::Newline(n) => Step::Newline(self.translate(n)), - &Step::Utf16Char(r) => Step::Utf16Char(self.translate_range(r)), + match *x { + Step::Newline(n) => Step::Newline(self.translate(n)), + Step::Utf16Char(r) => Step::Utf16Char(self.translate_range(r)), } } } diff --git a/crates/ra_text_edit/src/test_utils.rs b/crates/ra_text_edit/src/test_utils.rs index 9e21b24f6..2dc0e71af 100644 --- a/crates/ra_text_edit/src/test_utils.rs +++ b/crates/ra_text_edit/src/test_utils.rs @@ -42,8 +42,8 @@ pub fn arb_text_edit(text: &str) -> BoxedStrategy { .prop_flat_map(|cuts| { let strategies: Vec<_> = cuts .chunks(2) - .map(|chunk| match chunk { - &[from, to] => { + .map(|chunk| match *chunk { + [from, to] => { let range = TextRange::from_to(from, to); Just(AtomTextEdit::delete(range)) .boxed() @@ -54,7 +54,7 @@ pub fn arb_text_edit(text: &str) -> BoxedStrategy { ) .boxed() } - &[x] => arb_text().prop_map(move |text| AtomTextEdit::insert(x, text)).boxed(), + [x] => arb_text().prop_map(move |text| AtomTextEdit::insert(x, text)).boxed(), _ => unreachable!(), }) .collect(); -- cgit v1.2.3 From 63e37f95366131359282186286664b7b70038844 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Mon, 3 Jun 2019 10:13:19 -0400 Subject: Fix clippy::expect_fun_call --- crates/test_utils/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 7f55779cf..8bb3b3937 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -318,7 +318,7 @@ pub fn project_dir() -> PathBuf { /// so this should always be correct. pub fn read_text(path: &Path) -> String { fs::read_to_string(path) - .expect(&format!("File at {:?} should be valid", path)) + .unwrap_or_else(|_| panic!("File at {:?} should be valid", path)) .replace("\r\n", "\n") } -- cgit v1.2.3 From 4e449fb0b0d5fca99d0aaf481539fa9ec662a8c2 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Tue, 4 Jun 2019 01:43:12 -0400 Subject: Fix clippy::if_same_then_else --- crates/ra_parser/src/grammar/expressions.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index bb6c78b5f..99e32c4e8 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs @@ -454,6 +454,7 @@ fn method_call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { // x.1i32; // x.0x01; // } +#[allow(clippy::if_same_then_else)] fn field_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { assert!(p.at(T![.])); let m = lhs.precede(p); -- cgit v1.2.3 From fafca4cb11c0580ff10111d3ca44d4569932b125 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Tue, 4 Jun 2019 01:51:25 -0400 Subject: Fix clippy::ptr_arg --- crates/ra_hir/src/expr/validation.rs | 2 +- crates/ra_ide_api/src/completion/complete_scope.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/ra_hir/src/expr/validation.rs b/crates/ra_hir/src/expr/validation.rs index 2816144a7..0248b087d 100644 --- a/crates/ra_hir/src/expr/validation.rs +++ b/crates/ra_hir/src/expr/validation.rs @@ -44,7 +44,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> { &mut self, id: ExprId, _path: &Option, - fields: &Vec, + fields: &[StructLitField], spread: &Option, db: &impl HirDatabase, ) { diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs index 2473e58b4..0f8cfaae8 100644 --- a/crates/ra_ide_api/src/completion/complete_scope.rs +++ b/crates/ra_ide_api/src/completion/complete_scope.rs @@ -49,7 +49,7 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) { } } -fn build_import_label(name: &str, path: &Vec) -> String { +fn build_import_label(name: &str, path: &[SmolStr]) -> String { let mut buf = String::with_capacity(64); buf.push_str(name); buf.push_str(" ("); @@ -58,7 +58,7 @@ fn build_import_label(name: &str, path: &Vec) -> String { buf } -fn fmt_import_path(path: &Vec, buf: &mut String) { +fn fmt_import_path(path: &[SmolStr], buf: &mut String) { let mut segments = path.iter(); if let Some(s) = segments.next() { buf.push_str(&s); -- cgit v1.2.3 From 6095e3fe19da289580ba6feb46b643ff52276568 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Tue, 4 Jun 2019 02:06:19 -0400 Subject: Fix clippy::unnecessary_mut_passed --- crates/ra_lsp_server/tests/heavy_tests/support.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/crates/ra_lsp_server/tests/heavy_tests/support.rs b/crates/ra_lsp_server/tests/heavy_tests/support.rs index 955d283dd..22a5a610d 100644 --- a/crates/ra_lsp_server/tests/heavy_tests/support.rs +++ b/crates/ra_lsp_server/tests/heavy_tests/support.rs @@ -95,13 +95,8 @@ impl Server { "test server", 128, move |mut msg_receiver, mut msg_sender| { - main_loop( - roots, - InitializationOptions::default(), - &mut msg_receiver, - &mut msg_sender, - ) - .unwrap() + main_loop(roots, InitializationOptions::default(), &msg_receiver, &msg_sender) + .unwrap() }, ); let res = Server { req_id: Cell::new(1), dir, messages: Default::default(), worker }; -- cgit v1.2.3 From 7bcd8d6290f63a725a62cc7da0269c08c1b6c99b Mon Sep 17 00:00:00 2001 From: Alan Du Date: Tue, 4 Jun 2019 02:17:44 -0400 Subject: Fix clippy::unused_mut --- crates/ra_lsp_server/tests/heavy_tests/support.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_lsp_server/tests/heavy_tests/support.rs b/crates/ra_lsp_server/tests/heavy_tests/support.rs index 22a5a610d..75912afdd 100644 --- a/crates/ra_lsp_server/tests/heavy_tests/support.rs +++ b/crates/ra_lsp_server/tests/heavy_tests/support.rs @@ -94,7 +94,7 @@ impl Server { let worker = Worker::::spawn( "test server", 128, - move |mut msg_receiver, mut msg_sender| { + move |msg_receiver, msg_sender| { main_loop(roots, InitializationOptions::default(), &msg_receiver, &msg_sender) .unwrap() }, -- cgit v1.2.3 From 619a61529878f82daf7aed571bc4f6a10bd6dd9f Mon Sep 17 00:00:00 2001 From: Alan Du Date: Tue, 4 Jun 2019 02:28:22 -0400 Subject: Fix clippy::len_zero --- crates/ra_hir/src/path.rs | 4 ++-- crates/ra_ide_api/src/line_index.rs | 4 ++-- crates/ra_mbe/src/syntax_bridge.rs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/ra_hir/src/path.rs b/crates/ra_hir/src/path.rs index 1b129c752..67afd5027 100644 --- a/crates/ra_hir/src/path.rs +++ b/crates/ra_hir/src/path.rs @@ -116,7 +116,7 @@ impl Path { /// `true` if this path is just a standalone `self` pub fn is_self(&self) -> bool { - self.kind == PathKind::Self_ && self.segments.len() == 0 + self.kind == PathKind::Self_ && self.segments.is_empty() } /// If this path is a single identifier, like `foo`, return its name. @@ -140,7 +140,7 @@ impl GenericArgs { args.push(GenericArg::Type(type_ref)); } // lifetimes and assoc type args ignored for now - if args.len() > 0 { + if !args.is_empty() { Some(GenericArgs { args }) } else { None diff --git a/crates/ra_ide_api/src/line_index.rs b/crates/ra_ide_api/src/line_index.rs index fd33d6767..087dfafed 100644 --- a/crates/ra_ide_api/src/line_index.rs +++ b/crates/ra_ide_api/src/line_index.rs @@ -41,7 +41,7 @@ impl LineIndex { newlines.push(curr_row); // Save any utf-16 characters seen in the previous line - if utf16_chars.len() > 0 { + if !utf16_chars.is_empty() { utf16_lines.insert(line, utf16_chars); utf16_chars = Vec::new(); } @@ -61,7 +61,7 @@ impl LineIndex { } // Save any utf-16 characters seen in the last line - if utf16_chars.len() > 0 { + if !utf16_chars.is_empty() { utf16_lines.insert(line, utf16_chars); } diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 0edb6f9a2..e3f93b23c 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -292,7 +292,7 @@ fn delim_to_str(d: tt::Delimiter, closing: bool) -> SmolStr { }; let idx = closing as usize; - let text = if texts.len() > 0 { &texts[idx..texts.len() - (1 - idx)] } else { "" }; + let text = if !texts.is_empty() { &texts[idx..texts.len() - (1 - idx)] } else { "" }; text.into() } -- cgit v1.2.3 From fb592d76aa24e8fe74694038c283f56ea83ab568 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Tue, 4 Jun 2019 02:28:50 -0400 Subject: Fix clippy::into_iter_on_ref --- crates/ra_hir/src/expr/validation.rs | 2 +- crates/ra_hir/src/ty/infer.rs | 2 +- crates/ra_ide_api/src/folding_ranges.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/ra_hir/src/expr/validation.rs b/crates/ra_hir/src/expr/validation.rs index 0248b087d..592f558f2 100644 --- a/crates/ra_hir/src/expr/validation.rs +++ b/crates/ra_hir/src/expr/validation.rs @@ -57,7 +57,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> { _ => return, }; - let lit_fields: FxHashSet<_> = fields.into_iter().map(|f| &f.name).collect(); + let lit_fields: FxHashSet<_> = fields.iter().map(|f| &f.name).collect(); let missed_fields: Vec = struct_def .fields(db) .iter() diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 1723921e6..58cfcd8a2 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -1044,7 +1044,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { Expr::StructLit { path, fields, spread } => { let (ty, def_id) = self.resolve_variant(path.as_ref()); let substs = ty.substs().unwrap_or_else(Substs::empty); - for (field_idx, field) in fields.into_iter().enumerate() { + for (field_idx, field) in fields.iter().enumerate() { let field_ty = def_id .and_then(|it| match it.field(self.db, &field.name) { Some(field) => Some(field), diff --git a/crates/ra_ide_api/src/folding_ranges.rs b/crates/ra_ide_api/src/folding_ranges.rs index b50bbee38..4400ff232 100644 --- a/crates/ra_ide_api/src/folding_ranges.rs +++ b/crates/ra_ide_api/src/folding_ranges.rs @@ -205,7 +205,7 @@ mod tests { "The amount of fold kinds is different than the expected amount" ); for ((fold, range), fold_kind) in - folds.into_iter().zip(ranges.into_iter()).zip(fold_kinds.into_iter()) + folds.iter().zip(ranges.into_iter()).zip(fold_kinds.iter()) { assert_eq!(fold.range.start(), range.start()); assert_eq!(fold.range.end(), range.end()); -- cgit v1.2.3 From 21d7964bc3f0b923aac081d9ab854c3fc0c8b0b7 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Tue, 4 Jun 2019 02:29:50 -0400 Subject: Fix clippy::useless_format --- crates/ra_ide_api/src/diagnostics.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index a2a8c1e4f..35b3d77df 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -109,7 +109,7 @@ fn check_unnecessary_braces_in_use_statement( acc.push(Diagnostic { range, - message: format!("Unnecessary braces in use statement"), + message: "Unnecessary braces in use statement".to_string(), severity: Severity::WeakWarning, fix: Some(SourceChange::source_file_edit( "Remove unnecessary braces", @@ -155,7 +155,7 @@ fn check_struct_shorthand_initialization( acc.push(Diagnostic { range: named_field.syntax().range(), - message: format!("Shorthand struct initialization"), + message: "Shorthand struct initialization".to_string(), severity: Severity::WeakWarning, fix: Some(SourceChange::source_file_edit( "use struct shorthand initialization", -- cgit v1.2.3 From 6939011b273c8ac63a29faed6febef5a94035d40 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Tue, 4 Jun 2019 02:31:17 -0400 Subject: Fix clippy::assign_op_pattern --- crates/ra_ide_api/src/call_info.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs index 5d43282fd..0d1f92ca6 100644 --- a/crates/ra_ide_api/src/call_info.rs +++ b/crates/ra_ide_api/src/call_info.rs @@ -64,7 +64,7 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option Date: Tue, 4 Jun 2019 02:32:02 -0400 Subject: Fix clippy::new_without_default --- crates/ra_ide_api/src/hover.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index 9c35db2a4..f56965ef5 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -18,6 +18,12 @@ pub struct HoverResult { exact: bool, } +impl Default for HoverResult { + fn default() -> Self { + HoverResult::new() + } +} + impl HoverResult { pub fn new() -> HoverResult { HoverResult { -- cgit v1.2.3 From ed3d93b875f25da6f81b8a107a8c200311240627 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Tue, 4 Jun 2019 02:38:13 -0400 Subject: Fix clippy::single_char_pattern --- crates/ra_assists/src/introduce_variable.rs | 4 ++-- crates/ra_hir/src/expr/validation.rs | 7 ++----- crates/ra_project_model/src/lib.rs | 2 +- crates/ra_syntax/src/ast/extensions.rs | 2 +- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/crates/ra_assists/src/introduce_variable.rs b/crates/ra_assists/src/introduce_variable.rs index fb7333c8c..28467d341 100644 --- a/crates/ra_assists/src/introduce_variable.rs +++ b/crates/ra_assists/src/introduce_variable.rs @@ -57,9 +57,9 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx) -> Option if text.starts_with("\r\n") { buf.push_str("\r\n"); buf.push_str(text.trim_start_matches("\r\n")); - } else if text.starts_with("\n") { + } else if text.starts_with('\n') { buf.push_str("\n"); - buf.push_str(text.trim_start_matches("\n")); + buf.push_str(text.trim_start_matches('\n')); } else { buf.push_str(text); } diff --git a/crates/ra_hir/src/expr/validation.rs b/crates/ra_hir/src/expr/validation.rs index 592f558f2..a1b2641da 100644 --- a/crates/ra_hir/src/expr/validation.rs +++ b/crates/ra_hir/src/expr/validation.rs @@ -31,11 +31,8 @@ impl<'a, 'b> ExprValidator<'a, 'b> { pub(crate) fn validate_body(&mut self, db: &impl HirDatabase) { let body = self.func.body(db); for e in body.exprs() { - match e { - (id, Expr::StructLit { path, fields, spread }) => { - self.validate_struct_literal(id, path, fields, spread, db) - } - _ => (), + if let (id, Expr::StructLit { path, fields, spread }) = e { + self.validate_struct_literal(id, path, fields, spread, db); } } } diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 63eb7041e..4ae7f685c 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -70,7 +70,7 @@ impl ProjectRoot { }) }; - let hidden = dir_path.components().any(|c| c.as_str().starts_with(".")); + let hidden = dir_path.components().any(|c| c.as_str().starts_with('.')); !is_ignored && !hidden } diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index e4c99784c..930b2d9fa 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs @@ -78,7 +78,7 @@ impl ast::Attr { if attr.kind() == IDENT { let key = attr.as_token()?.text().clone(); let val_node = tt_node.children_with_tokens().find(|t| t.kind() == STRING)?; - let val = val_node.as_token()?.text().trim_start_matches("\"").trim_end_matches("\""); + let val = val_node.as_token()?.text().trim_start_matches('"').trim_end_matches('"'); Some((key, SmolStr::new(val))) } else { None -- cgit v1.2.3 From 40424d4222d4630bc53294d10f1718f2d3d300de Mon Sep 17 00:00:00 2001 From: Alan Du Date: Mon, 3 Jun 2019 10:21:08 -0400 Subject: Fix clippy::identity_conversion --- crates/ra_batch/src/lib.rs | 4 ++-- crates/ra_hir/src/adt.rs | 2 +- crates/ra_hir/src/impl_block.rs | 1 - crates/ra_hir/src/nameres.rs | 2 +- crates/ra_hir/src/nameres/collector.rs | 2 +- crates/ra_hir/src/nameres/raw.rs | 2 +- crates/ra_hir/src/source_binder.rs | 11 ++++------ crates/ra_hir/src/ty/infer.rs | 6 +++--- crates/ra_ide_api/src/call_info.rs | 3 +-- crates/ra_ide_api/src/typing.rs | 2 +- crates/ra_lsp_server/src/cargo_target_spec.rs | 2 +- crates/ra_lsp_server/src/main_loop.rs | 4 ++-- crates/ra_lsp_server/src/world.rs | 29 +++++++++++--------------- crates/ra_mbe/src/mbe_expander.rs | 24 ++++++++++----------- crates/ra_mbe/src/syntax_bridge.rs | 7 ++++--- crates/ra_project_model/src/cargo_workspace.rs | 7 +++---- 16 files changed, 49 insertions(+), 59 deletions(-) diff --git a/crates/ra_batch/src/lib.rs b/crates/ra_batch/src/lib.rs index a4a8462de..a445dcb4d 100644 --- a/crates/ra_batch/src/lib.rs +++ b/crates/ra_batch/src/lib.rs @@ -34,10 +34,10 @@ impl salsa::Database for BatchDatabase { } fn vfs_file_to_id(f: ra_vfs::VfsFile) -> FileId { - FileId(f.0.into()) + FileId(f.0) } fn vfs_root_to_id(r: ra_vfs::VfsRoot) -> SourceRootId { - SourceRootId(r.0.into()) + SourceRootId(r.0) } impl BatchDatabase { diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index 45a12dd4d..38ff1d6f6 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -36,7 +36,7 @@ impl AdtDef { impl Struct { pub(crate) fn variant_data(&self, db: &impl DefDatabase) -> Arc { - db.struct_data((*self).into()).variant_data.clone() + db.struct_data(*self).variant_data.clone() } } diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index ba90e67e9..a0d3b33fe 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs @@ -202,7 +202,6 @@ impl ModuleImplBlocks { }; let (file_id, module_source) = m.module.definition_source(db); - let file_id: HirFileId = file_id.into(); let node = match &module_source { ModuleSource::SourceFile(node) => node.syntax(), ModuleSource::Module(node) => { diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index d649aa820..d822f7d93 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -468,7 +468,7 @@ impl CrateDefMap { ); return ResolvePathResult::with( - Either::Left(PerNs::types((*s).into())), + Either::Left(PerNs::types(*s)), ReachedFixedPoint::Yes, Some(i), ); diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index 693c3fe8e..3bfef799d 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -556,7 +556,7 @@ where fn define_def(&mut self, def: &raw::DefData) { let module = Module { krate: self.def_collector.def_map.krate, module_id: self.module_id }; - let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id.into()); + let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id); macro_rules! def { ($kind:ident, $ast_id:ident) => { diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs index 21935dbb9..a0afe282c 100644 --- a/crates/ra_hir/src/nameres/raw.rs +++ b/crates/ra_hir/src/nameres/raw.rs @@ -69,7 +69,7 @@ impl RawItems { ) -> (Arc, Arc) { let mut collector = RawItemsCollector { raw_items: RawItems::default(), - source_ast_id_map: db.ast_id_map(file_id.into()), + source_ast_id_map: db.ast_id_map(file_id), source_map: ImportSourceMap::default(), }; if let Some(node) = db.parse_or_expand(file_id) { diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 75ed2de6c..6a5799622 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -48,8 +48,8 @@ pub fn module_from_declaration( pub fn module_from_position(db: &impl HirDatabase, position: FilePosition) -> Option { let file = db.parse(position.file_id).tree; match find_node_at_offset::(file.syntax(), position.offset) { - Some(m) if !m.has_semi() => module_from_inline(db, position.file_id.into(), m), - _ => module_from_file_id(db, position.file_id.into()), + Some(m) if !m.has_semi() => module_from_inline(db, position.file_id, m), + _ => module_from_file_id(db, position.file_id), } } @@ -72,9 +72,9 @@ pub fn module_from_child_node( child: &SyntaxNode, ) -> Option { if let Some(m) = child.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) { - module_from_inline(db, file_id.into(), m) + module_from_inline(db, file_id, m) } else { - module_from_file_id(db, file_id.into()) + module_from_file_id(db, file_id) } } @@ -99,14 +99,12 @@ pub fn struct_from_module( struct_def: &ast::StructDef, ) -> Struct { let (file_id, _) = module.definition_source(db); - let file_id = file_id.into(); let ctx = LocationCtx::new(db, module, file_id); Struct { id: ctx.to_def(struct_def) } } pub fn enum_from_module(db: &impl HirDatabase, module: Module, enum_def: &ast::EnumDef) -> Enum { let (file_id, _) = module.definition_source(db); - let file_id = file_id.into(); let ctx = LocationCtx::new(db, module, file_id); Enum { id: ctx.to_def(enum_def) } } @@ -117,7 +115,6 @@ pub fn trait_from_module( trait_def: &ast::TraitDef, ) -> Trait { let (file_id, _) = module.definition_source(db); - let file_id = file_id.into(); let ctx = LocationCtx::new(db, module, file_id); Trait { id: ctx.to_def(trait_def) } } diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 58cfcd8a2..5edc59f18 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -539,7 +539,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } })?; - resolved = Resolution::Def(item.into()); + resolved = Resolution::Def(item); } match resolved { @@ -762,7 +762,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { _ => &Ty::Unknown, }; let subty = self.infer_pat(*pat, expectation, default_bm); - Ty::apply_one(TypeCtor::Ref(*mutability), subty.into()) + Ty::apply_one(TypeCtor::Ref(*mutability), subty) } Pat::TupleStruct { path: ref p, args: ref subpats } => { self.infer_tuple_struct_pat(p.as_ref(), subpats, expected, default_bm) @@ -790,7 +790,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let bound_ty = match mode { BindingMode::Ref(mutability) => { - Ty::apply_one(TypeCtor::Ref(mutability), inner_ty.clone().into()) + Ty::apply_one(TypeCtor::Ref(mutability), inner_ty.clone()) } BindingMode::Move => inner_ty.clone(), }; diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs index 0d1f92ca6..bd08e183d 100644 --- a/crates/ra_ide_api/src/call_info.rs +++ b/crates/ra_ide_api/src/call_info.rs @@ -21,8 +21,7 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option { //FIXME: apply subst - let (callable_def, _subst) = - analyzer.type_of(db, expr.expr()?.into())?.as_callable()?; + let (callable_def, _subst) = analyzer.type_of(db, expr.expr()?)?.as_callable()?; match callable_def { hir::CallableDef::Function(it) => it, //FIXME: handle other callables diff --git a/crates/ra_ide_api/src/typing.rs b/crates/ra_ide_api/src/typing.rs index 63bc0cf88..3e35d8352 100644 --- a/crates/ra_ide_api/src/typing.rs +++ b/crates/ra_ide_api/src/typing.rs @@ -110,7 +110,7 @@ pub(crate) fn on_dot_typed(db: &RootDatabase, position: FilePosition) -> Option< let mut edit = TextEditBuilder::default(); edit.replace( TextRange::from_to(position.offset - current_indent_len, position.offset), - target_indent.into(), + target_indent, ); let res = SourceChange::source_file_edit_from("reindent dot", position.file_id, edit.finish()) diff --git a/crates/ra_lsp_server/src/cargo_target_spec.rs b/crates/ra_lsp_server/src/cargo_target_spec.rs index 742361155..082ac8609 100644 --- a/crates/ra_lsp_server/src/cargo_target_spec.rs +++ b/crates/ra_lsp_server/src/cargo_target_spec.rs @@ -64,7 +64,7 @@ impl CargoTargetSpec { None => return Ok(None), }; let file_id = world.analysis().crate_root(crate_id)?; - let path = world.vfs.read().file2path(ra_vfs::VfsFile(file_id.0.into())); + let path = world.vfs.read().file2path(ra_vfs::VfsFile(file_id.0)); let res = world.workspaces.iter().find_map(|ws| match ws { project_model::ProjectWorkspace::Cargo { cargo, .. } => { let tgt = cargo.target_by_root(&path)?; diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs index 6080a3a4e..090fb9b1b 100644 --- a/crates/ra_lsp_server/src/main_loop.rs +++ b/crates/ra_lsp_server/src/main_loop.rs @@ -384,7 +384,7 @@ fn on_notification( if let Some(file_id) = state.vfs.write().add_file_overlay(&path, params.text_document.text) { - subs.add_sub(FileId(file_id.0.into())); + subs.add_sub(FileId(file_id.0)); } return Ok(()); } @@ -406,7 +406,7 @@ fn on_notification( let uri = params.text_document.uri; let path = uri.to_file_path().map_err(|()| format_err!("invalid uri: {}", uri))?; if let Some(file_id) = state.vfs.write().remove_file_overlay(path.as_path()) { - subs.remove_sub(FileId(file_id.0.into())); + subs.remove_sub(FileId(file_id.0)); } let params = req::PublishDiagnosticsParams { uri, diagnostics: Vec::new() }; let not = RawNotification::new::(¶ms); diff --git a/crates/ra_lsp_server/src/world.rs b/crates/ra_lsp_server/src/world.rs index e0d2f6306..cd8df4fdb 100644 --- a/crates/ra_lsp_server/src/world.rs +++ b/crates/ra_lsp_server/src/world.rs @@ -60,14 +60,14 @@ impl WorldState { for r in vfs_roots { let vfs_root_path = vfs.root2path(r); let is_local = folder_roots.iter().any(|it| vfs_root_path.starts_with(it)); - change.add_root(SourceRootId(r.0.into()), is_local); + change.add_root(SourceRootId(r.0), is_local); } // Create crate graph from all the workspaces let mut crate_graph = CrateGraph::default(); let mut load = |path: &std::path::Path| { let vfs_file = vfs.load(path); - vfs_file.map(|f| FileId(f.0.into())) + vfs_file.map(|f| FileId(f.0)) }; for ws in workspaces.iter() { crate_graph.extend(ws.to_crate_graph(&mut load)); @@ -105,29 +105,24 @@ impl WorldState { if is_local { self.roots_to_scan -= 1; for (file, path, text) in files { - change.add_file( - SourceRootId(root.0.into()), - FileId(file.0.into()), - path, - text, - ); + change.add_file(SourceRootId(root.0), FileId(file.0), path, text); } } else { let files = files .into_iter() - .map(|(vfsfile, path, text)| (FileId(vfsfile.0.into()), path, text)) + .map(|(vfsfile, path, text)| (FileId(vfsfile.0), path, text)) .collect(); - libs.push((SourceRootId(root.0.into()), files)); + libs.push((SourceRootId(root.0), files)); } } VfsChange::AddFile { root, file, path, text } => { - change.add_file(SourceRootId(root.0.into()), FileId(file.0.into()), path, text); + change.add_file(SourceRootId(root.0), FileId(file.0), path, text); } VfsChange::RemoveFile { root, file, path } => { - change.remove_file(SourceRootId(root.0.into()), FileId(file.0.into()), path) + change.remove_file(SourceRootId(root.0), FileId(file.0), path) } VfsChange::ChangeFile { file, text } => { - change.change_file(FileId(file.0.into()), text); + change.change_file(FileId(file.0), text); } } } @@ -178,18 +173,18 @@ impl WorldSnapshot { message: "Rust file outside current workspace is not supported yet.".to_string(), }) })?; - Ok(FileId(file.0.into())) + Ok(FileId(file.0)) } pub fn file_id_to_uri(&self, id: FileId) -> Result { - let path = self.vfs.read().file2path(VfsFile(id.0.into())); + let path = self.vfs.read().file2path(VfsFile(id.0)); let url = Url::from_file_path(&path) .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?; Ok(url) } pub fn path_to_uri(&self, root: SourceRootId, path: &RelativePathBuf) -> Result { - let base = self.vfs.read().root2path(VfsRoot(root.0.into())); + let base = self.vfs.read().root2path(VfsRoot(root.0)); let path = path.to_path(base); let url = Url::from_file_path(&path) .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?; @@ -212,7 +207,7 @@ impl WorldSnapshot { } pub fn workspace_root_for(&self, file_id: FileId) -> Option<&Path> { - let path = self.vfs.read().file2path(VfsFile(file_id.0.into())); + let path = self.vfs.read().file2path(VfsFile(file_id.0)); self.workspaces.iter().find_map(|ws| ws.workspace_root_for(&path)) } } diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index a0bd0c5f8..7cfb47f7a 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs @@ -206,48 +206,48 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result { let path = input.eat_path().ok_or(ExpandError::UnexpectedToken)?.clone(); - res.inner.insert(text.clone(), Binding::Simple(path.into())); + res.inner.insert(text.clone(), Binding::Simple(path)); } "expr" => { let expr = input.eat_expr().ok_or(ExpandError::UnexpectedToken)?.clone(); - res.inner.insert(text.clone(), Binding::Simple(expr.into())); + res.inner.insert(text.clone(), Binding::Simple(expr)); } "ty" => { let ty = input.eat_ty().ok_or(ExpandError::UnexpectedToken)?.clone(); - res.inner.insert(text.clone(), Binding::Simple(ty.into())); + res.inner.insert(text.clone(), Binding::Simple(ty)); } "pat" => { let pat = input.eat_pat().ok_or(ExpandError::UnexpectedToken)?.clone(); - res.inner.insert(text.clone(), Binding::Simple(pat.into())); + res.inner.insert(text.clone(), Binding::Simple(pat)); } "stmt" => { let pat = input.eat_stmt().ok_or(ExpandError::UnexpectedToken)?.clone(); - res.inner.insert(text.clone(), Binding::Simple(pat.into())); + res.inner.insert(text.clone(), Binding::Simple(pat)); } "block" => { let block = input.eat_block().ok_or(ExpandError::UnexpectedToken)?.clone(); - res.inner.insert(text.clone(), Binding::Simple(block.into())); + res.inner.insert(text.clone(), Binding::Simple(block)); } "meta" => { let meta = input.eat_meta().ok_or(ExpandError::UnexpectedToken)?.clone(); - res.inner.insert(text.clone(), Binding::Simple(meta.into())); + res.inner.insert(text.clone(), Binding::Simple(meta)); } "tt" => { let token = input.eat().ok_or(ExpandError::UnexpectedToken)?.clone(); - res.inner.insert(text.clone(), Binding::Simple(token.into())); + res.inner.insert(text.clone(), Binding::Simple(token)); } "item" => { let item = input.eat_item().ok_or(ExpandError::UnexpectedToken)?.clone(); - res.inner.insert(text.clone(), Binding::Simple(item.into())); + res.inner.insert(text.clone(), Binding::Simple(item)); } "lifetime" => { let lifetime = input.eat_lifetime().ok_or(ExpandError::UnexpectedToken)?.clone(); - res.inner.insert(text.clone(), Binding::Simple(lifetime.into())); + res.inner.insert(text.clone(), Binding::Simple(lifetime)); } "literal" => { let literal = @@ -262,7 +262,7 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result(token: &ra_syntax::SyntaxToken<'a>) -> Option Date: Mon, 3 Jun 2019 10:27:51 -0400 Subject: Fix clippy::or_fun_call --- crates/ra_assists/src/auto_import.rs | 4 ++-- crates/ra_assists/src/change_visibility.rs | 2 +- crates/ra_hir/src/lang_item.rs | 2 +- crates/ra_hir/src/nameres.rs | 17 +++++++++++------ crates/ra_hir/src/ty.rs | 2 +- crates/ra_hir/src/ty/infer.rs | 2 +- crates/ra_ide_api/src/completion/complete_pattern.rs | 2 +- crates/ra_ide_api/src/extend_selection.rs | 2 +- crates/ra_lsp_server/src/main.rs | 2 +- crates/ra_mbe/src/mbe_expander.rs | 14 ++++++-------- crates/ra_mbe/src/mbe_parser.rs | 4 ++-- crates/ra_syntax/src/validation/unescape.rs | 2 +- 12 files changed, 29 insertions(+), 26 deletions(-) diff --git a/crates/ra_assists/src/auto_import.rs b/crates/ra_assists/src/auto_import.rs index 1566cf179..9617beb5c 100644 --- a/crates/ra_assists/src/auto_import.rs +++ b/crates/ra_assists/src/auto_import.rs @@ -334,7 +334,7 @@ fn best_action_for_target<'b, 'a: 'b>( .filter_map(ast::UseItem::use_tree) .map(|u| walk_use_tree_for_best_action(&mut storage, None, u, target)) .fold(None, |best, a| { - best.and_then(|best| Some(*ImportAction::better(&best, &a))).or(Some(a)) + best.and_then(|best| Some(*ImportAction::better(&best, &a))).or_else(|| Some(a)) }); match best_action { @@ -347,7 +347,7 @@ fn best_action_for_target<'b, 'a: 'b>( let anchor = container .children() .find(|n| n.range().start() < anchor.range().start()) - .or(Some(anchor)); + .or_else(|| Some(anchor)); return ImportAction::add_new_use(anchor, false); } diff --git a/crates/ra_assists/src/change_visibility.rs b/crates/ra_assists/src/change_visibility.rs index 620f534b5..4eb24d27f 100644 --- a/crates/ra_assists/src/change_visibility.rs +++ b/crates/ra_assists/src/change_visibility.rs @@ -59,7 +59,7 @@ fn vis_offset(node: &SyntaxNode) -> TextUnit { }) .next() .map(|it| it.range().start()) - .unwrap_or(node.range().start()) + .unwrap_or_else(|| node.range().start()) } fn change_vis(mut ctx: AssistCtx, vis: &ast::Visibility) -> Option { diff --git a/crates/ra_hir/src/lang_item.rs b/crates/ra_hir/src/lang_item.rs index 4cacaeba4..684fbc068 100644 --- a/crates/ra_hir/src/lang_item.rs +++ b/crates/ra_hir/src/lang_item.rs @@ -95,7 +95,7 @@ impl LangItems { .nth(0); if let Some(lang_item_name) = lang_item_name { let imp = ImplBlock::from_id(*module, impl_id); - self.items.entry(lang_item_name).or_insert(LangItemTarget::ImplBlock(imp)); + self.items.entry(lang_item_name).or_insert_with(|| LangItemTarget::ImplBlock(imp)); } } diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index d822f7d93..7bddee9b9 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -332,7 +332,8 @@ impl CrateDefMap { let name = path.expand_macro_expr()?; // search local first // FIXME: Remove public_macros check when we have a correct local_macors implementation - let local = self.public_macros.get(&name).or(self.local_macros.get(&name)).map(|it| *it); + let local = + self.public_macros.get(&name).or_else(|| self.local_macros.get(&name)).map(|it| *it); if local.is_some() { return local; } @@ -479,8 +480,10 @@ impl CrateDefMap { } fn resolve_name_in_crate_root_or_extern_prelude(&self, name: &Name) -> ItemOrMacro { - let from_crate_root = - self[self.root].scope.get_item_or_macro(name).unwrap_or(Either::Left(PerNs::none())); + let from_crate_root = self[self.root] + .scope + .get_item_or_macro(name) + .unwrap_or_else(|| Either::Left(PerNs::none())); let from_extern_prelude = self.resolve_name_in_extern_prelude(name); or(from_crate_root, Either::Left(from_extern_prelude)) @@ -505,8 +508,10 @@ impl CrateDefMap { // - current module / scope // - extern prelude // - std prelude - let from_scope = - self[module].scope.get_item_or_macro(name).unwrap_or(Either::Left(PerNs::none()));; + let from_scope = self[module] + .scope + .get_item_or_macro(name) + .unwrap_or_else(|| Either::Left(PerNs::none()));; let from_extern_prelude = self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)); let from_prelude = self.resolve_in_prelude(db, name); @@ -525,7 +530,7 @@ impl CrateDefMap { } else { db.crate_def_map(prelude.krate)[prelude.module_id].scope.get_item_or_macro(name) }; - resolution.unwrap_or(Either::Left(PerNs::none())) + resolution.unwrap_or_else(|| Either::Left(PerNs::none())) } else { Either::Left(PerNs::none()) } diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 76d34c12b..4a37e0268 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -451,7 +451,7 @@ impl Ty { /// Substitutes `Ty::Bound` vars (as opposed to type parameters). pub fn subst_bound_vars(self, substs: &Substs) -> Ty { self.fold(&mut |ty| match ty { - Ty::Bound(idx) => substs.get(idx as usize).cloned().unwrap_or(Ty::Bound(idx)), + Ty::Bound(idx) => substs.get(idx as usize).cloned().unwrap_or_else(|| Ty::Bound(idx)), ty => ty, }) } diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 5edc59f18..905fe9f0e 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -462,7 +462,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let mut resolved = if remaining_index.is_none() { def.take_values()? } else { def.take_types()? }; - let remaining_index = remaining_index.unwrap_or(path.segments.len()); + let remaining_index = remaining_index.unwrap_or_else(|| path.segments.len()); let mut actual_def_ty: Option = None; let krate = resolver.krate()?; diff --git a/crates/ra_ide_api/src/completion/complete_pattern.rs b/crates/ra_ide_api/src/completion/complete_pattern.rs index 74833a756..6655a05a7 100644 --- a/crates/ra_ide_api/src/completion/complete_pattern.rs +++ b/crates/ra_ide_api/src/completion/complete_pattern.rs @@ -10,7 +10,7 @@ pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) { let names = ctx.analyzer.all_names(ctx.db); for (name, res) in names.into_iter() { let r = res.as_ref(); - let def = match r.take_types().or(r.take_values()) { + let def = match r.take_types().or_else(|| r.take_values()) { Some(hir::Resolution::Def(def)) => def, _ => continue, }; diff --git a/crates/ra_ide_api/src/extend_selection.rs b/crates/ra_ide_api/src/extend_selection.rs index 00c445310..a713b762c 100644 --- a/crates/ra_ide_api/src/extend_selection.rs +++ b/crates/ra_ide_api/src/extend_selection.rs @@ -95,7 +95,7 @@ fn extend_single_word_in_comment_or_string( } let start_idx = before.rfind(non_word_char)? as u32; - let end_idx = after.find(non_word_char).unwrap_or(after.len()) as u32; + let end_idx = after.find(non_word_char).unwrap_or_else(|| after.len()) as u32; let from: TextUnit = (start_idx + 1).into(); let to: TextUnit = (cursor_position + end_idx).into(); diff --git a/crates/ra_lsp_server/src/main.rs b/crates/ra_lsp_server/src/main.rs index b0b70df5c..a0df32dde 100644 --- a/crates/ra_lsp_server/src/main.rs +++ b/crates/ra_lsp_server/src/main.rs @@ -49,7 +49,7 @@ fn main_inner() -> Result<()> { let opts = params .initialization_options .and_then(|v| InitializationOptions::deserialize(v).ok()) - .unwrap_or(InitializationOptions::default()); + .unwrap_or_default(); ra_lsp_server::main_loop(workspace_roots, opts, r, s) })?; diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index 7cfb47f7a..55a6ecf58 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs @@ -105,17 +105,15 @@ impl Bindings { } fn get(&self, name: &SmolStr, nesting: &[usize]) -> Result<&tt::TokenTree, ExpandError> { - let mut b = self - .inner - .get(name) - .ok_or(ExpandError::BindingError(format!("could not find binding `{}`", name)))?; + let mut b = self.inner.get(name).ok_or_else(|| { + ExpandError::BindingError(format!("could not find binding `{}`", name)) + })?; for &idx in nesting.iter() { b = match b { Binding::Simple(_) => break, - Binding::Nested(bs) => bs.get(idx).ok_or(ExpandError::BindingError(format!( - "could not find nested binding `{}`", - name - )))?, + Binding::Nested(bs) => bs.get(idx).ok_or_else(|| { + ExpandError::BindingError(format!("could not find nested binding `{}`", name)) + })?, Binding::Empty => { return Err(ExpandError::BindingError(format!( "could not find empty binding `{}`", diff --git a/crates/ra_mbe/src/mbe_parser.rs b/crates/ra_mbe/src/mbe_parser.rs index d8fe293c7..dca16b537 100644 --- a/crates/ra_mbe/src/mbe_parser.rs +++ b/crates/ra_mbe/src/mbe_parser.rs @@ -125,8 +125,8 @@ fn parse_repeat(p: &mut TtCursor, transcriber: bool) -> Result Date: Tue, 4 Jun 2019 02:58:22 -0400 Subject: Fix clippy::while_let_loop --- crates/ra_assists/src/ast_editor.rs | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs index cabb3d862..de0529b32 100644 --- a/crates/ra_assists/src/ast_editor.rs +++ b/crates/ra_assists/src/ast_editor.rs @@ -212,21 +212,17 @@ impl AstEditor { } pub fn strip_attrs_and_docs(&mut self) { - loop { - if let Some(start) = self - .ast() - .syntax() - .children_with_tokens() - .find(|it| it.kind() == ATTR || it.kind() == COMMENT) - { - let end = match start.next_sibling_or_token() { - Some(el) if el.kind() == WHITESPACE => el, - Some(_) | None => start, - }; - self.ast = self.replace_children(RangeInclusive::new(start, end), iter::empty()); - } else { - break; - } + while let Some(start) = self + .ast() + .syntax() + .children_with_tokens() + .find(|it| it.kind() == ATTR || it.kind() == COMMENT) + { + let end = match start.next_sibling_or_token() { + Some(el) if el.kind() == WHITESPACE => el, + Some(_) | None => start, + }; + self.ast = self.replace_children(RangeInclusive::new(start, end), iter::empty()); } } } -- cgit v1.2.3 From 9b54b06ee3867cf632434b9cbb06d64d0d739cff Mon Sep 17 00:00:00 2001 From: Alan Du Date: Tue, 4 Jun 2019 03:07:23 -0400 Subject: Fix clippy::option_map_or_none --- crates/ra_hir/src/nameres.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index 7bddee9b9..c59c30579 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -406,7 +406,7 @@ impl CrateDefMap { }; for (i, segment) in segments { - let curr = match curr_per_ns.as_ref().left().map_or(None, |m| m.as_ref().take_types()) { + let curr = match curr_per_ns.as_ref().left().and_then(|m| m.as_ref().take_types()) { Some(r) => r, None => { // we still have path segments left, but the path so far -- cgit v1.2.3 From 02b6f871f174f510cd258c5064dcbe08cadd11d3 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Tue, 4 Jun 2019 03:07:57 -0400 Subject: Fix clippy::iter_cloned_collect --- crates/ra_hir/src/nameres.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index c59c30579..8b798d6c9 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -422,10 +422,8 @@ impl CrateDefMap { curr_per_ns = match curr { ModuleDef::Module(module) => { if module.krate != self.krate { - let path = Path { - segments: path.segments[i..].iter().cloned().collect(), - kind: PathKind::Self_, - }; + let path = + Path { segments: path.segments[i..].to_vec(), kind: PathKind::Self_ }; log::debug!("resolving {:?} in other crate", path); let defp_map = db.crate_def_map(module.krate); let (def, s) = -- cgit v1.2.3 From aa30c4909ebb1e85f1591f465c9e2875aa4d394e Mon Sep 17 00:00:00 2001 From: Alan Du Date: Tue, 4 Jun 2019 18:05:38 -0400 Subject: Allow clippy::redundant_pattern_matching --- crates/tools/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs index b424eb701..2446fdf28 100644 --- a/crates/tools/src/lib.rs +++ b/crates/tools/src/lib.rs @@ -146,9 +146,10 @@ pub fn run_clippy() -> Result<()> { let allowed_lints = [ "clippy::collapsible_if", - "clippy::nonminimal_bool", - "clippy::needless_pass_by_value", "clippy::map_clone", // FIXME: remove when Iterator::copied stabilizes (1.36.0) + "clippy::needless_pass_by_value", + "clippy::nonminimal_bool", + "clippy::redundant_pattern_matching", ]; run( &format!( -- cgit v1.2.3