Skip to content

Commit 5deb907

Browse files
bors[bot]alanhdu
andcommitted
Merge #1374
1374: Implement `cargo lint` and fix some clippy errors r=alanhdu a=alanhdu This creates a `cargo lint` command that runs clippy with certain lints disabled. I've also gone ahead and fixed some of the lint errors, although there are many more still to go. cc #848 Co-authored-by: Alan Du <[email protected]>
2 parents 8bd0e84 + aa30c49 commit 5deb907

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+227
-229
lines changed

.cargo/config

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ install-code = "run --package tools --bin tools -- install-code"
1212
# Formats the full repository or installs the git hook to do it automatically.
1313
format = "run --package tools --bin tools -- format"
1414
format-hook = "run --package tools --bin tools -- format-hook"
15+
# Run clippy
16+
lint = "run --package tools --bin tools -- lint"
17+
1518
# Runs the fuzzing test suite (currently only parser)
1619
fuzz-tests = "run --package tools --bin tools -- fuzz-tests"
1720

crates/ra_assists/src/ast_editor.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -212,21 +212,17 @@ impl AstEditor<ast::FnDef> {
212212
}
213213

214214
pub fn strip_attrs_and_docs(&mut self) {
215-
loop {
216-
if let Some(start) = self
217-
.ast()
218-
.syntax()
219-
.children_with_tokens()
220-
.find(|it| it.kind() == ATTR || it.kind() == COMMENT)
221-
{
222-
let end = match start.next_sibling_or_token() {
223-
Some(el) if el.kind() == WHITESPACE => el,
224-
Some(_) | None => start,
225-
};
226-
self.ast = self.replace_children(RangeInclusive::new(start, end), iter::empty());
227-
} else {
228-
break;
229-
}
215+
while let Some(start) = self
216+
.ast()
217+
.syntax()
218+
.children_with_tokens()
219+
.find(|it| it.kind() == ATTR || it.kind() == COMMENT)
220+
{
221+
let end = match start.next_sibling_or_token() {
222+
Some(el) if el.kind() == WHITESPACE => el,
223+
Some(_) | None => start,
224+
};
225+
self.ast = self.replace_children(RangeInclusive::new(start, end), iter::empty());
230226
}
231227
}
232228
}

crates/ra_assists/src/auto_import.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ fn best_action_for_target<'b, 'a: 'b>(
334334
.filter_map(ast::UseItem::use_tree)
335335
.map(|u| walk_use_tree_for_best_action(&mut storage, None, u, target))
336336
.fold(None, |best, a| {
337-
best.and_then(|best| Some(*ImportAction::better(&best, &a))).or(Some(a))
337+
best.and_then(|best| Some(*ImportAction::better(&best, &a))).or_else(|| Some(a))
338338
});
339339

340340
match best_action {
@@ -347,7 +347,7 @@ fn best_action_for_target<'b, 'a: 'b>(
347347
let anchor = container
348348
.children()
349349
.find(|n| n.range().start() < anchor.range().start())
350-
.or(Some(anchor));
350+
.or_else(|| Some(anchor));
351351

352352
return ImportAction::add_new_use(anchor, false);
353353
}

crates/ra_assists/src/change_visibility.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn vis_offset(node: &SyntaxNode) -> TextUnit {
5959
})
6060
.next()
6161
.map(|it| it.range().start())
62-
.unwrap_or(node.range().start())
62+
.unwrap_or_else(|| node.range().start())
6363
}
6464

6565
fn change_vis(mut ctx: AssistCtx<impl HirDatabase>, vis: &ast::Visibility) -> Option<Assist> {

crates/ra_assists/src/introduce_variable.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option
5757
if text.starts_with("\r\n") {
5858
buf.push_str("\r\n");
5959
buf.push_str(text.trim_start_matches("\r\n"));
60-
} else if text.starts_with("\n") {
60+
} else if text.starts_with('\n') {
6161
buf.push_str("\n");
62-
buf.push_str(text.trim_start_matches("\n"));
62+
buf.push_str(text.trim_start_matches('\n'));
6363
} else {
6464
buf.push_str(text);
6565
}

crates/ra_batch/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ impl salsa::Database for BatchDatabase {
3434
}
3535

3636
fn vfs_file_to_id(f: ra_vfs::VfsFile) -> FileId {
37-
FileId(f.0.into())
37+
FileId(f.0)
3838
}
3939
fn vfs_root_to_id(r: ra_vfs::VfsRoot) -> SourceRootId {
40-
SourceRootId(r.0.into())
40+
SourceRootId(r.0)
4141
}
4242

4343
impl BatchDatabase {

crates/ra_cli/src/analysis_stats.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,16 @@ pub fn run(verbose: bool, path: &str, only: Option<&str>) -> Result<()> {
3131

3232
for decl in module.declarations(&db) {
3333
num_decls += 1;
34-
match decl {
35-
ModuleDef::Function(f) => funcs.push(f),
36-
_ => {}
34+
if let ModuleDef::Function(f) = decl {
35+
funcs.push(f);
3736
}
3837
}
3938

4039
for impl_block in module.impl_blocks(&db) {
4140
for item in impl_block.items(&db) {
4241
num_decls += 1;
43-
match item {
44-
ImplItem::Method(f) => funcs.push(f),
45-
_ => {}
42+
if let ImplItem::Method(f) = item {
43+
funcs.push(f);
4644
}
4745
}
4846
}

crates/ra_hir/src/adt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl AdtDef {
3636

3737
impl Struct {
3838
pub(crate) fn variant_data(&self, db: &impl DefDatabase) -> Arc<VariantData> {
39-
db.struct_data((*self).into()).variant_data.clone()
39+
db.struct_data(*self).variant_data.clone()
4040
}
4141
}
4242

crates/ra_hir/src/code_model.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,8 @@ impl Module {
281281

282282
for impl_block in self.impl_blocks(db) {
283283
for item in impl_block.items(db) {
284-
match item {
285-
crate::ImplItem::Method(f) => f.diagnostics(db, sink),
286-
_ => (),
284+
if let crate::ImplItem::Method(f) = item {
285+
f.diagnostics(db, sink);
287286
}
288287
}
289288
}

crates/ra_hir/src/expr/validation.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
3131
pub(crate) fn validate_body(&mut self, db: &impl HirDatabase) {
3232
let body = self.func.body(db);
3333
for e in body.exprs() {
34-
match e {
35-
(id, Expr::StructLit { path, fields, spread }) => {
36-
self.validate_struct_literal(id, path, fields, spread, db)
37-
}
38-
_ => (),
34+
if let (id, Expr::StructLit { path, fields, spread }) = e {
35+
self.validate_struct_literal(id, path, fields, spread, db);
3936
}
4037
}
4138
}
@@ -44,7 +41,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
4441
&mut self,
4542
id: ExprId,
4643
_path: &Option<Path>,
47-
fields: &Vec<StructLitField>,
44+
fields: &[StructLitField],
4845
spread: &Option<ExprId>,
4946
db: &impl HirDatabase,
5047
) {
@@ -57,7 +54,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
5754
_ => return,
5855
};
5956

60-
let lit_fields: FxHashSet<_> = fields.into_iter().map(|f| &f.name).collect();
57+
let lit_fields: FxHashSet<_> = fields.iter().map(|f| &f.name).collect();
6158
let missed_fields: Vec<Name> = struct_def
6259
.fields(db)
6360
.iter()

crates/ra_hir/src/impl_block.rs

-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ impl ModuleImplBlocks {
202202
};
203203

204204
let (file_id, module_source) = m.module.definition_source(db);
205-
let file_id: HirFileId = file_id.into();
206205
let node = match &module_source {
207206
ModuleSource::SourceFile(node) => node.syntax(),
208207
ModuleSource::Module(node) => {

crates/ra_hir/src/lang_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl LangItems {
9595
.nth(0);
9696
if let Some(lang_item_name) = lang_item_name {
9797
let imp = ImplBlock::from_id(*module, impl_id);
98-
self.items.entry(lang_item_name).or_insert(LangItemTarget::ImplBlock(imp));
98+
self.items.entry(lang_item_name).or_insert_with(|| LangItemTarget::ImplBlock(imp));
9999
}
100100
}
101101

crates/ra_hir/src/nameres.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ impl CrateDefMap {
332332
let name = path.expand_macro_expr()?;
333333
// search local first
334334
// FIXME: Remove public_macros check when we have a correct local_macors implementation
335-
let local = self.public_macros.get(&name).or(self.local_macros.get(&name)).map(|it| *it);
335+
let local =
336+
self.public_macros.get(&name).or_else(|| self.local_macros.get(&name)).map(|it| *it);
336337
if local.is_some() {
337338
return local;
338339
}
@@ -405,7 +406,7 @@ impl CrateDefMap {
405406
};
406407

407408
for (i, segment) in segments {
408-
let curr = match curr_per_ns.as_ref().left().map_or(None, |m| m.as_ref().take_types()) {
409+
let curr = match curr_per_ns.as_ref().left().and_then(|m| m.as_ref().take_types()) {
409410
Some(r) => r,
410411
None => {
411412
// we still have path segments left, but the path so far
@@ -421,10 +422,8 @@ impl CrateDefMap {
421422
curr_per_ns = match curr {
422423
ModuleDef::Module(module) => {
423424
if module.krate != self.krate {
424-
let path = Path {
425-
segments: path.segments[i..].iter().cloned().collect(),
426-
kind: PathKind::Self_,
427-
};
425+
let path =
426+
Path { segments: path.segments[i..].to_vec(), kind: PathKind::Self_ };
428427
log::debug!("resolving {:?} in other crate", path);
429428
let defp_map = db.crate_def_map(module.krate);
430429
let (def, s) =
@@ -468,7 +467,7 @@ impl CrateDefMap {
468467
);
469468

470469
return ResolvePathResult::with(
471-
Either::Left(PerNs::types((*s).into())),
470+
Either::Left(PerNs::types(*s)),
472471
ReachedFixedPoint::Yes,
473472
Some(i),
474473
);
@@ -479,8 +478,10 @@ impl CrateDefMap {
479478
}
480479

481480
fn resolve_name_in_crate_root_or_extern_prelude(&self, name: &Name) -> ItemOrMacro {
482-
let from_crate_root =
483-
self[self.root].scope.get_item_or_macro(name).unwrap_or(Either::Left(PerNs::none()));
481+
let from_crate_root = self[self.root]
482+
.scope
483+
.get_item_or_macro(name)
484+
.unwrap_or_else(|| Either::Left(PerNs::none()));
484485
let from_extern_prelude = self.resolve_name_in_extern_prelude(name);
485486

486487
or(from_crate_root, Either::Left(from_extern_prelude))
@@ -505,8 +506,10 @@ impl CrateDefMap {
505506
// - current module / scope
506507
// - extern prelude
507508
// - std prelude
508-
let from_scope =
509-
self[module].scope.get_item_or_macro(name).unwrap_or(Either::Left(PerNs::none()));;
509+
let from_scope = self[module]
510+
.scope
511+
.get_item_or_macro(name)
512+
.unwrap_or_else(|| Either::Left(PerNs::none()));;
510513
let from_extern_prelude =
511514
self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it));
512515
let from_prelude = self.resolve_in_prelude(db, name);
@@ -525,7 +528,7 @@ impl CrateDefMap {
525528
} else {
526529
db.crate_def_map(prelude.krate)[prelude.module_id].scope.get_item_or_macro(name)
527530
};
528-
resolution.unwrap_or(Either::Left(PerNs::none()))
531+
resolution.unwrap_or_else(|| Either::Left(PerNs::none()))
529532
} else {
530533
Either::Left(PerNs::none())
531534
}

crates/ra_hir/src/nameres/collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ where
556556

557557
fn define_def(&mut self, def: &raw::DefData) {
558558
let module = Module { krate: self.def_collector.def_map.krate, module_id: self.module_id };
559-
let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id.into());
559+
let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id);
560560

561561
macro_rules! def {
562562
($kind:ident, $ast_id:ident) => {

crates/ra_hir/src/nameres/raw.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl RawItems {
6969
) -> (Arc<RawItems>, Arc<ImportSourceMap>) {
7070
let mut collector = RawItemsCollector {
7171
raw_items: RawItems::default(),
72-
source_ast_id_map: db.ast_id_map(file_id.into()),
72+
source_ast_id_map: db.ast_id_map(file_id),
7373
source_map: ImportSourceMap::default(),
7474
};
7575
if let Some(node) = db.parse_or_expand(file_id) {

crates/ra_hir/src/path.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl Path {
116116

117117
/// `true` if this path is just a standalone `self`
118118
pub fn is_self(&self) -> bool {
119-
self.kind == PathKind::Self_ && self.segments.len() == 0
119+
self.kind == PathKind::Self_ && self.segments.is_empty()
120120
}
121121

122122
/// If this path is a single identifier, like `foo`, return its name.
@@ -140,7 +140,7 @@ impl GenericArgs {
140140
args.push(GenericArg::Type(type_ref));
141141
}
142142
// lifetimes and assoc type args ignored for now
143-
if args.len() > 0 {
143+
if !args.is_empty() {
144144
Some(GenericArgs { args })
145145
} else {
146146
None

crates/ra_hir/src/source_binder.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ pub fn module_from_declaration(
4848
pub fn module_from_position(db: &impl HirDatabase, position: FilePosition) -> Option<Module> {
4949
let file = db.parse(position.file_id).tree;
5050
match find_node_at_offset::<ast::Module>(file.syntax(), position.offset) {
51-
Some(m) if !m.has_semi() => module_from_inline(db, position.file_id.into(), m),
52-
_ => module_from_file_id(db, position.file_id.into()),
51+
Some(m) if !m.has_semi() => module_from_inline(db, position.file_id, m),
52+
_ => module_from_file_id(db, position.file_id),
5353
}
5454
}
5555

@@ -72,9 +72,9 @@ pub fn module_from_child_node(
7272
child: &SyntaxNode,
7373
) -> Option<Module> {
7474
if let Some(m) = child.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) {
75-
module_from_inline(db, file_id.into(), m)
75+
module_from_inline(db, file_id, m)
7676
} else {
77-
module_from_file_id(db, file_id.into())
77+
module_from_file_id(db, file_id)
7878
}
7979
}
8080

@@ -99,14 +99,12 @@ pub fn struct_from_module(
9999
struct_def: &ast::StructDef,
100100
) -> Struct {
101101
let (file_id, _) = module.definition_source(db);
102-
let file_id = file_id.into();
103102
let ctx = LocationCtx::new(db, module, file_id);
104103
Struct { id: ctx.to_def(struct_def) }
105104
}
106105

107106
pub fn enum_from_module(db: &impl HirDatabase, module: Module, enum_def: &ast::EnumDef) -> Enum {
108107
let (file_id, _) = module.definition_source(db);
109-
let file_id = file_id.into();
110108
let ctx = LocationCtx::new(db, module, file_id);
111109
Enum { id: ctx.to_def(enum_def) }
112110
}
@@ -117,7 +115,6 @@ pub fn trait_from_module(
117115
trait_def: &ast::TraitDef,
118116
) -> Trait {
119117
let (file_id, _) = module.definition_source(db);
120-
let file_id = file_id.into();
121118
let ctx = LocationCtx::new(db, module, file_id);
122119
Trait { id: ctx.to_def(trait_def) }
123120
}

crates/ra_hir/src/traits.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,10 @@ impl TraitItemsIndex {
7777
pub(crate) fn trait_items_index(db: &impl DefDatabase, module: Module) -> TraitItemsIndex {
7878
let mut index = TraitItemsIndex { traits_by_def: FxHashMap::default() };
7979
for decl in module.declarations(db) {
80-
match decl {
81-
crate::ModuleDef::Trait(tr) => {
82-
for item in tr.trait_data(db).items() {
83-
index.traits_by_def.insert(*item, tr);
84-
}
80+
if let crate::ModuleDef::Trait(tr) = decl {
81+
for item in tr.trait_data(db).items() {
82+
index.traits_by_def.insert(*item, tr);
8583
}
86-
_ => {}
8784
}
8885
}
8986
index

crates/ra_hir/src/ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ impl Ty {
451451
/// Substitutes `Ty::Bound` vars (as opposed to type parameters).
452452
pub fn subst_bound_vars(self, substs: &Substs) -> Ty {
453453
self.fold(&mut |ty| match ty {
454-
Ty::Bound(idx) => substs.get(idx as usize).cloned().unwrap_or(Ty::Bound(idx)),
454+
Ty::Bound(idx) => substs.get(idx as usize).cloned().unwrap_or_else(|| Ty::Bound(idx)),
455455
ty => ty,
456456
})
457457
}

0 commit comments

Comments
 (0)