Skip to content

Commit ae14328

Browse files
committed
Lint debug prints and disallowed types with clippy
1 parent 850ba2f commit ae14328

File tree

63 files changed

+169
-229
lines changed

Some content is hidden

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

63 files changed

+169
-229
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ jobs:
105105

106106
- name: clippy
107107
if: matrix.os == 'ubuntu-latest'
108-
run: cargo clippy --all-targets
108+
run: cargo clippy --all-targets -- -D clippy::disallowed_macros -D clippy::dbg_macro -D clippy::todo -D clippy::print_stdout -D clippy::print_stderr
109109

110110
# Weird targets to catch non-portable code
111111
rust-cross:

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ len_without_is_empty = "allow"
164164
enum_variant_names = "allow"
165165
# Builder pattern disagrees
166166
new_ret_no_self = "allow"
167+
# Has a bunch of false positives
168+
useless_asref = "allow"
167169

168170
## Following lints should be tackled at some point
169171
borrowed_box = "allow"
@@ -178,9 +180,12 @@ type_complexity = "allow"
178180
wrong_self_convention = "allow"
179181

180182
## warn at following lints
183+
# CI raises these to deny
181184
dbg_macro = "warn"
182185
todo = "warn"
183-
unimplemented = "allow"
186+
print_stdout = "warn"
187+
print_stderr = "warn"
188+
184189
rc_buffer = "warn"
185190
# FIXME enable this, we use this pattern a lot so its annoying work ...
186191
# str_to_string = "warn"

clippy.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
disallowed-types = [
2+
{ path = "std::collections::HashMap", reason = "use FxHashMap" },
3+
{ path = "std::collections::HashSet", reason = "use FxHashSet" },
4+
{ path = "std::collections::hash_map::RandomState", reason = "use BuildHasherDefault<FxHasher>"}
5+
]

crates/flycheck/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,7 @@ impl CargoActor {
493493
// Skip certain kinds of messages to only spend time on what's useful
494494
JsonMessage::Cargo(message) => match message {
495495
cargo_metadata::Message::CompilerArtifact(artifact) if !artifact.fresh => {
496-
self.sender
497-
.send(CargoMessage::CompilerArtifact(Box::new(artifact)))
498-
.unwrap();
496+
self.sender.send(CargoMessage::CompilerArtifact(artifact)).unwrap();
499497
}
500498
cargo_metadata::Message::CompilerMessage(msg) => {
501499
self.sender.send(CargoMessage::Diagnostic(msg.message)).unwrap();
@@ -539,8 +537,9 @@ impl CargoActor {
539537
}
540538
}
541539

540+
#[allow(clippy::large_enum_variant)]
542541
enum CargoMessage {
543-
CompilerArtifact(Box<cargo_metadata::Artifact>),
542+
CompilerArtifact(cargo_metadata::Artifact),
544543
Diagnostic(Diagnostic),
545544
}
546545

crates/hir-def/src/body/lower.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,10 +1980,7 @@ fn pat_literal_to_hir(lit: &ast::LiteralPat) -> Option<(Literal, ast::Literal)>
19801980
let ast_lit = lit.literal()?;
19811981
let mut hir_lit: Literal = ast_lit.kind().into();
19821982
if lit.minus_token().is_some() {
1983-
let Some(h) = hir_lit.negate() else {
1984-
return None;
1985-
};
1986-
hir_lit = h;
1983+
hir_lit = hir_lit.negate()?;
19871984
}
19881985
Some((hir_lit, ast_lit))
19891986
}

crates/hir-def/src/item_scope.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,15 @@ impl ItemScope {
222222
self.declarations.iter().copied()
223223
}
224224

225-
pub fn extern_crate_decls(
226-
&self,
227-
) -> impl Iterator<Item = ExternCrateId> + ExactSizeIterator + '_ {
225+
pub fn extern_crate_decls(&self) -> impl ExactSizeIterator<Item = ExternCrateId> + '_ {
228226
self.extern_crate_decls.iter().copied()
229227
}
230228

231-
pub fn use_decls(&self) -> impl Iterator<Item = UseId> + ExactSizeIterator + '_ {
229+
pub fn use_decls(&self) -> impl ExactSizeIterator<Item = UseId> + '_ {
232230
self.use_decls.iter().copied()
233231
}
234232

235-
pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ {
233+
pub fn impls(&self) -> impl ExactSizeIterator<Item = ImplId> + '_ {
236234
self.impls.iter().copied()
237235
}
238236

crates/hir-ty/src/layout/tests.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use std::collections::HashMap;
2-
31
use chalk_ir::{AdtId, TyKind};
42
use either::Either;
53
use hir_def::db::DefDatabase;
4+
use rustc_hash::FxHashMap;
65
use test_fixture::WithFixture;
76
use triomphe::Arc;
87

@@ -16,7 +15,7 @@ use crate::{
1615
mod closure;
1716

1817
fn current_machine_data_layout() -> String {
19-
project_model::target_data_layout::get(None, None, &HashMap::default()).unwrap()
18+
project_model::target_data_layout::get(None, None, &FxHashMap::default()).unwrap()
2019
}
2120

2221
fn eval_goal(ra_fixture: &str, minicore: &str) -> Result<Arc<Layout>, LayoutError> {

crates/hir-ty/src/tests.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod regression;
1010
mod simple;
1111
mod traits;
1212

13-
use std::{collections::HashMap, env};
13+
use std::env;
1414

1515
use base_db::{FileRange, SourceDatabaseExt};
1616
use expect_test::Expect;
@@ -25,6 +25,7 @@ use hir_def::{
2525
};
2626
use hir_expand::{db::ExpandDatabase, InFile};
2727
use once_cell::race::OnceBool;
28+
use rustc_hash::FxHashMap;
2829
use stdx::format_to;
2930
use syntax::{
3031
ast::{self, AstNode, HasName},
@@ -90,9 +91,9 @@ fn check_impl(ra_fixture: &str, allow_none: bool, only_types: bool, display_sour
9091
let (db, files) = TestDB::with_many_files(ra_fixture);
9192

9293
let mut had_annotations = false;
93-
let mut mismatches = HashMap::new();
94-
let mut types = HashMap::new();
95-
let mut adjustments = HashMap::<_, Vec<_>>::new();
94+
let mut mismatches = FxHashMap::default();
95+
let mut types = FxHashMap::default();
96+
let mut adjustments = FxHashMap::<_, Vec<_>>::default();
9697
for (file_id, annotations) in db.extract_annotations() {
9798
for (range, expected) in annotations {
9899
let file_range = FileRange { file_id, range };

crates/hir-ty/src/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ struct LoggingRustIrDatabaseLoggingOnDrop<'a>(LoggingRustIrDatabase<Interner, Ch
187187

188188
impl Drop for LoggingRustIrDatabaseLoggingOnDrop<'_> {
189189
fn drop(&mut self) {
190-
eprintln!("chalk program:\n{}", self.0);
190+
tracing::info!("chalk program:\n{}", self.0);
191191
}
192192
}
193193

crates/hir/src/diagnostics.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,7 @@ impl AnyDiagnostic {
546546
source_map.pat_syntax(pat).expect("unexpected synthetic");
547547

548548
// cast from Either<Pat, SelfParam> -> Either<_, Pat>
549-
let Some(ptr) = AstPtr::try_from_raw(value.syntax_node_ptr()) else {
550-
return None;
551-
};
549+
let ptr = AstPtr::try_from_raw(value.syntax_node_ptr())?;
552550
InFile { file_id, value: ptr }
553551
}
554552
};

crates/ide-assists/src/handlers/desugar_doc_comment.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ use crate::{
2727
pub(crate) fn desugar_doc_comment(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
2828
let comment = ctx.find_token_at_offset::<ast::Comment>()?;
2929
// Only allow doc comments
30-
let Some(placement) = comment.kind().doc else {
31-
return None;
32-
};
30+
let placement = comment.kind().doc?;
3331

3432
// Only allow comments which are alone on their line
3533
if let Some(prev) = comment.syntax().prev_token() {

crates/ide-assists/src/handlers/extract_module.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
use std::{
2-
collections::{HashMap, HashSet},
3-
iter,
4-
};
1+
use std::iter;
52

63
use hir::{HasSource, HirFileIdExt, ModuleSource};
74
use ide_db::{
85
assists::{AssistId, AssistKind},
96
base_db::FileId,
107
defs::{Definition, NameClass, NameRefClass},
118
search::{FileReference, SearchScope},
9+
FxHashMap, FxHashSet,
1210
};
1311
use itertools::Itertools;
1412
use smallvec::SmallVec;
@@ -235,9 +233,9 @@ impl Module {
235233
fn get_usages_and_record_fields(
236234
&self,
237235
ctx: &AssistContext<'_>,
238-
) -> (HashMap<FileId, Vec<(TextRange, String)>>, Vec<SyntaxNode>) {
236+
) -> (FxHashMap<FileId, Vec<(TextRange, String)>>, Vec<SyntaxNode>) {
239237
let mut adt_fields = Vec::new();
240-
let mut refs: HashMap<FileId, Vec<(TextRange, String)>> = HashMap::new();
238+
let mut refs: FxHashMap<FileId, Vec<(TextRange, String)>> = FxHashMap::default();
241239

242240
//Here impl is not included as each item inside impl will be tied to the parent of
243241
//implementing block(a struct, enum, etc), if the parent is in selected module, it will
@@ -320,7 +318,7 @@ impl Module {
320318
&self,
321319
ctx: &AssistContext<'_>,
322320
node_def: Definition,
323-
refs_in_files: &mut HashMap<FileId, Vec<(TextRange, String)>>,
321+
refs_in_files: &mut FxHashMap<FileId, Vec<(TextRange, String)>>,
324322
) {
325323
for (file_id, references) in node_def.usages(&ctx.sema).all() {
326324
let source_file = ctx.sema.parse(file_id);
@@ -400,7 +398,7 @@ impl Module {
400398
ctx: &AssistContext<'_>,
401399
) -> Vec<TextRange> {
402400
let mut import_paths_to_be_removed: Vec<TextRange> = vec![];
403-
let mut node_set: HashSet<String> = HashSet::new();
401+
let mut node_set: FxHashSet<String> = FxHashSet::default();
404402

405403
for item in self.body_items.clone() {
406404
for x in item.syntax().descendants() {

crates/ide-assists/src/handlers/generate_delegate_methods.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
use std::collections::HashSet;
2-
31
use hir::{self, HasCrate, HasVisibility};
4-
use ide_db::path_transform::PathTransform;
2+
use ide_db::{path_transform::PathTransform, FxHashSet};
53
use syntax::{
64
ast::{
75
self, edit_in_place::Indent, make, AstNode, HasGenericParams, HasName, HasVisibility as _,
@@ -71,7 +69,7 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
7169

7270
let sema_field_ty = ctx.sema.resolve_type(&field_ty)?;
7371
let mut methods = vec![];
74-
let mut seen_names = HashSet::new();
72+
let mut seen_names = FxHashSet::default();
7573

7674
for ty in sema_field_ty.autoderef(ctx.db()) {
7775
let krate = ty.krate(ctx.db());

crates/ide-assists/src/handlers/generate_delegate_trait.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,7 @@ fn generate_args_for_impl(
502502
trait_params: &Option<GenericParamList>,
503503
old_trait_args: &FxHashSet<String>,
504504
) -> Option<ast::GenericArgList> {
505-
let Some(old_impl_args) = old_impl_gpl.map(|gpl| gpl.to_generic_args().generic_args()) else {
506-
return None;
507-
};
505+
let old_impl_args = old_impl_gpl.map(|gpl| gpl.to_generic_args().generic_args())?;
508506
// Create pairs of the args of `self_ty` and corresponding `field_ty` to
509507
// form the substitution list
510508
let mut arg_substs = FxHashMap::default();

crates/ide-assists/src/handlers/inline_type_alias.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
// - Remove unused aliases if there are no longer any users, see inline_call.rs.
44

55
use hir::{HasSource, PathResolution};
6+
use ide_db::FxHashMap;
67
use ide_db::{
78
defs::Definition, imports::insert_use::ast_to_remove_for_path_in_use_stmt,
89
search::FileReference,
910
};
1011
use itertools::Itertools;
11-
use std::collections::HashMap;
1212
use syntax::{
1313
ast::{self, make, HasGenericParams, HasName},
1414
ted, AstNode, NodeOrToken, SyntaxNode,
@@ -189,14 +189,14 @@ fn inline(alias_def: &ast::TypeAlias, alias_instance: &ast::PathType) -> Option<
189189
Some(repl)
190190
}
191191

192-
struct LifetimeMap(HashMap<String, ast::Lifetime>);
192+
struct LifetimeMap(FxHashMap<String, ast::Lifetime>);
193193

194194
impl LifetimeMap {
195195
fn new(
196196
instance_args: &Option<ast::GenericArgList>,
197197
alias_generics: &ast::GenericParamList,
198198
) -> Option<Self> {
199-
let mut inner = HashMap::new();
199+
let mut inner = FxHashMap::default();
200200

201201
let wildcard_lifetime = make::lifetime("'_");
202202
let lifetimes = alias_generics
@@ -231,14 +231,14 @@ impl LifetimeMap {
231231
}
232232
}
233233

234-
struct ConstAndTypeMap(HashMap<String, SyntaxNode>);
234+
struct ConstAndTypeMap(FxHashMap<String, SyntaxNode>);
235235

236236
impl ConstAndTypeMap {
237237
fn new(
238238
instance_args: &Option<ast::GenericArgList>,
239239
alias_generics: &ast::GenericParamList,
240240
) -> Option<Self> {
241-
let mut inner = HashMap::new();
241+
let mut inner = FxHashMap::default();
242242
let instance_generics = generic_args_to_const_and_type_generics(instance_args);
243243
let alias_generics = generic_param_list_to_const_and_type_generics(alias_generics);
244244

crates/ide-assists/src/handlers/merge_match_arms.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use hir::Type;
2-
use std::{collections::HashMap, iter::successors};
2+
use ide_db::FxHashMap;
3+
use std::iter::successors;
34
use syntax::{
45
algo::neighbor,
56
ast::{self, AstNode, HasName},
@@ -95,7 +96,7 @@ fn contains_placeholder(a: &ast::MatchArm) -> bool {
9596
}
9697

9798
fn are_same_types(
98-
current_arm_types: &HashMap<String, Option<Type>>,
99+
current_arm_types: &FxHashMap<String, Option<Type>>,
99100
arm: &ast::MatchArm,
100101
ctx: &AssistContext<'_>,
101102
) -> bool {
@@ -114,11 +115,11 @@ fn are_same_types(
114115
fn get_arm_types(
115116
context: &AssistContext<'_>,
116117
arm: &ast::MatchArm,
117-
) -> HashMap<String, Option<Type>> {
118-
let mut mapping: HashMap<String, Option<Type>> = HashMap::new();
118+
) -> FxHashMap<String, Option<Type>> {
119+
let mut mapping: FxHashMap<String, Option<Type>> = FxHashMap::default();
119120

120121
fn recurse(
121-
map: &mut HashMap<String, Option<Type>>,
122+
map: &mut FxHashMap<String, Option<Type>>,
122123
ctx: &AssistContext<'_>,
123124
pat: &Option<ast::Pat>,
124125
) {

crates/ide-assists/src/handlers/remove_unused_imports.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::collections::{hash_map::Entry, HashMap};
1+
use std::collections::hash_map::Entry;
22

33
use hir::{HirFileIdExt, InFile, InRealFile, Module, ModuleSource};
44
use ide_db::{
55
base_db::FileRange,
66
defs::Definition,
77
search::{FileReference, ReferenceCategory, SearchScope},
8-
RootDatabase,
8+
FxHashMap, RootDatabase,
99
};
1010
use syntax::{ast, AstNode};
1111
use text_edit::TextRange;
@@ -44,7 +44,7 @@ pub(crate) fn remove_unused_imports(acc: &mut Assists, ctx: &AssistContext<'_>)
4444
let uses = uses_up.chain(uses_down).collect::<Vec<_>>();
4545

4646
// Maps use nodes to the scope that we should search through to find
47-
let mut search_scopes = HashMap::<Module, Vec<SearchScope>>::new();
47+
let mut search_scopes = FxHashMap::<Module, Vec<SearchScope>>::default();
4848

4949
// iterator over all unused use trees
5050
let mut unused = uses

crates/ide-assists/src/handlers/unwrap_result_return_type.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ pub(crate) fn unwrap_result_return_type(acc: &mut Assists, ctx: &AssistContext<'
4747
return None;
4848
}
4949

50-
let Some(ok_type) = unwrap_result_type(type_ref) else {
51-
return None;
52-
};
50+
let ok_type = unwrap_result_type(type_ref)?;
5351

5452
acc.add(
5553
AssistId("unwrap_result_return_type", AssistKind::RefactorRewrite),

crates/ide-assists/src/utils/suggest_name.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
//! This module contains functions to suggest names for expressions, functions and other items
22
3-
use std::collections::HashSet;
4-
53
use hir::Semantics;
6-
use ide_db::RootDatabase;
4+
use ide_db::{FxHashSet, RootDatabase};
75
use itertools::Itertools;
86
use stdx::to_lower_snake_case;
97
use syntax::{
@@ -78,7 +76,7 @@ pub(crate) fn for_unique_generic_name(
7876
ast::GenericParam::TypeParam(t) => t.name().unwrap().to_string(),
7977
p => p.to_string(),
8078
})
81-
.collect::<HashSet<_>>();
79+
.collect::<FxHashSet<_>>();
8280
let mut name = name.to_string();
8381
let base_len = name.len();
8482
let mut count = 0;

crates/ide-db/src/tests/sourcegen_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ fn unescape(s: &str) -> String {
241241
s.replace(r#"\""#, "").replace(r#"\n"#, "\n").replace(r#"\r"#, "")
242242
}
243243

244+
#[allow(clippy::print_stderr)]
244245
fn generate_descriptor_clippy(buf: &mut String, path: &Path) {
245246
let file_content = std::fs::read_to_string(path).unwrap();
246247
let mut clippy_lints: Vec<ClippyLint> = Vec::new();

0 commit comments

Comments
 (0)