Lint debug prints and disallowed types with clippy

This commit is contained in:
Lukas Wirth 2024-02-01 16:16:38 +01:00
parent 850ba2fb63
commit 9e8a0fae0c
64 changed files with 170 additions and 229 deletions

View file

@ -105,7 +105,7 @@ jobs:
- name: clippy
if: matrix.os == 'ubuntu-latest'
run: cargo clippy --all-targets
run: cargo clippy --all-targets -- -D clippy::disallowed_macros -D clippy::dbg_macro -D clippy::todo -D clippy::print_stdout -D clippy::print_stderr
# Weird targets to catch non-portable code
rust-cross:

1
Cargo.lock generated
View file

@ -1283,6 +1283,7 @@ dependencies = [
"object 0.32.0",
"paths",
"profile",
"rustc-hash",
"serde",
"serde_json",
"snap",

View file

@ -164,6 +164,8 @@ len_without_is_empty = "allow"
enum_variant_names = "allow"
# Builder pattern disagrees
new_ret_no_self = "allow"
# Has a bunch of false positives
useless_asref = "allow"
## Following lints should be tackled at some point
borrowed_box = "allow"
@ -178,9 +180,12 @@ type_complexity = "allow"
wrong_self_convention = "allow"
## warn at following lints
# CI raises these to deny
dbg_macro = "warn"
todo = "warn"
unimplemented = "allow"
print_stdout = "warn"
print_stderr = "warn"
rc_buffer = "warn"
# FIXME enable this, we use this pattern a lot so its annoying work ...
# str_to_string = "warn"

5
clippy.toml Normal file
View file

@ -0,0 +1,5 @@
disallowed-types = [
{ path = "std::collections::HashMap", reason = "use FxHashMap" },
{ path = "std::collections::HashSet", reason = "use FxHashSet" },
{ path = "std::collections::hash_map::RandomState", reason = "use BuildHasherDefault<FxHasher>"}
]

View file

@ -493,9 +493,7 @@ impl CargoActor {
// Skip certain kinds of messages to only spend time on what's useful
JsonMessage::Cargo(message) => match message {
cargo_metadata::Message::CompilerArtifact(artifact) if !artifact.fresh => {
self.sender
.send(CargoMessage::CompilerArtifact(Box::new(artifact)))
.unwrap();
self.sender.send(CargoMessage::CompilerArtifact(artifact)).unwrap();
}
cargo_metadata::Message::CompilerMessage(msg) => {
self.sender.send(CargoMessage::Diagnostic(msg.message)).unwrap();
@ -539,8 +537,9 @@ impl CargoActor {
}
}
#[allow(clippy::large_enum_variant)]
enum CargoMessage {
CompilerArtifact(Box<cargo_metadata::Artifact>),
CompilerArtifact(cargo_metadata::Artifact),
Diagnostic(Diagnostic),
}

View file

@ -1980,10 +1980,7 @@ fn pat_literal_to_hir(lit: &ast::LiteralPat) -> Option<(Literal, ast::Literal)>
let ast_lit = lit.literal()?;
let mut hir_lit: Literal = ast_lit.kind().into();
if lit.minus_token().is_some() {
let Some(h) = hir_lit.negate() else {
return None;
};
hir_lit = h;
hir_lit = hir_lit.negate()?;
}
Some((hir_lit, ast_lit))
}

View file

@ -222,17 +222,15 @@ impl ItemScope {
self.declarations.iter().copied()
}
pub fn extern_crate_decls(
&self,
) -> impl Iterator<Item = ExternCrateId> + ExactSizeIterator + '_ {
pub fn extern_crate_decls(&self) -> impl ExactSizeIterator<Item = ExternCrateId> + '_ {
self.extern_crate_decls.iter().copied()
}
pub fn use_decls(&self) -> impl Iterator<Item = UseId> + ExactSizeIterator + '_ {
pub fn use_decls(&self) -> impl ExactSizeIterator<Item = UseId> + '_ {
self.use_decls.iter().copied()
}
pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ {
pub fn impls(&self) -> impl ExactSizeIterator<Item = ImplId> + '_ {
self.impls.iter().copied()
}

View file

@ -1,8 +1,7 @@
use std::collections::HashMap;
use chalk_ir::{AdtId, TyKind};
use either::Either;
use hir_def::db::DefDatabase;
use rustc_hash::FxHashMap;
use test_fixture::WithFixture;
use triomphe::Arc;
@ -16,7 +15,7 @@ use crate::{
mod closure;
fn current_machine_data_layout() -> String {
project_model::target_data_layout::get(None, None, &HashMap::default()).unwrap()
project_model::target_data_layout::get(None, None, &FxHashMap::default()).unwrap()
}
fn eval_goal(ra_fixture: &str, minicore: &str) -> Result<Arc<Layout>, LayoutError> {

View file

@ -10,7 +10,7 @@ mod regression;
mod simple;
mod traits;
use std::{collections::HashMap, env};
use std::env;
use base_db::{FileRange, SourceDatabaseExt};
use expect_test::Expect;
@ -25,6 +25,7 @@ use hir_def::{
};
use hir_expand::{db::ExpandDatabase, InFile};
use once_cell::race::OnceBool;
use rustc_hash::FxHashMap;
use stdx::format_to;
use syntax::{
ast::{self, AstNode, HasName},
@ -90,9 +91,9 @@ fn check_impl(ra_fixture: &str, allow_none: bool, only_types: bool, display_sour
let (db, files) = TestDB::with_many_files(ra_fixture);
let mut had_annotations = false;
let mut mismatches = HashMap::new();
let mut types = HashMap::new();
let mut adjustments = HashMap::<_, Vec<_>>::new();
let mut mismatches = FxHashMap::default();
let mut types = FxHashMap::default();
let mut adjustments = FxHashMap::<_, Vec<_>>::default();
for (file_id, annotations) in db.extract_annotations() {
for (range, expected) in annotations {
let file_range = FileRange { file_id, range };

View file

@ -187,7 +187,7 @@ struct LoggingRustIrDatabaseLoggingOnDrop<'a>(LoggingRustIrDatabase<Interner, Ch
impl Drop for LoggingRustIrDatabaseLoggingOnDrop<'_> {
fn drop(&mut self) {
eprintln!("chalk program:\n{}", self.0);
tracing::info!("chalk program:\n{}", self.0);
}
}

View file

@ -546,9 +546,7 @@ impl AnyDiagnostic {
source_map.pat_syntax(pat).expect("unexpected synthetic");
// cast from Either<Pat, SelfParam> -> Either<_, Pat>
let Some(ptr) = AstPtr::try_from_raw(value.syntax_node_ptr()) else {
return None;
};
let ptr = AstPtr::try_from_raw(value.syntax_node_ptr())?;
InFile { file_id, value: ptr }
}
};

View file

@ -27,9 +27,7 @@ use crate::{
pub(crate) fn desugar_doc_comment(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
let comment = ctx.find_token_at_offset::<ast::Comment>()?;
// Only allow doc comments
let Some(placement) = comment.kind().doc else {
return None;
};
let placement = comment.kind().doc?;
// Only allow comments which are alone on their line
if let Some(prev) = comment.syntax().prev_token() {

View file

@ -1,7 +1,4 @@
use std::{
collections::{HashMap, HashSet},
iter,
};
use std::iter;
use hir::{HasSource, HirFileIdExt, ModuleSource};
use ide_db::{
@ -9,6 +6,7 @@ use ide_db::{
base_db::FileId,
defs::{Definition, NameClass, NameRefClass},
search::{FileReference, SearchScope},
FxHashMap, FxHashSet,
};
use itertools::Itertools;
use smallvec::SmallVec;
@ -235,9 +233,9 @@ impl Module {
fn get_usages_and_record_fields(
&self,
ctx: &AssistContext<'_>,
) -> (HashMap<FileId, Vec<(TextRange, String)>>, Vec<SyntaxNode>) {
) -> (FxHashMap<FileId, Vec<(TextRange, String)>>, Vec<SyntaxNode>) {
let mut adt_fields = Vec::new();
let mut refs: HashMap<FileId, Vec<(TextRange, String)>> = HashMap::new();
let mut refs: FxHashMap<FileId, Vec<(TextRange, String)>> = FxHashMap::default();
//Here impl is not included as each item inside impl will be tied to the parent of
//implementing block(a struct, enum, etc), if the parent is in selected module, it will
@ -320,7 +318,7 @@ impl Module {
&self,
ctx: &AssistContext<'_>,
node_def: Definition,
refs_in_files: &mut HashMap<FileId, Vec<(TextRange, String)>>,
refs_in_files: &mut FxHashMap<FileId, Vec<(TextRange, String)>>,
) {
for (file_id, references) in node_def.usages(&ctx.sema).all() {
let source_file = ctx.sema.parse(file_id);
@ -400,7 +398,7 @@ impl Module {
ctx: &AssistContext<'_>,
) -> Vec<TextRange> {
let mut import_paths_to_be_removed: Vec<TextRange> = vec![];
let mut node_set: HashSet<String> = HashSet::new();
let mut node_set: FxHashSet<String> = FxHashSet::default();
for item in self.body_items.clone() {
for x in item.syntax().descendants() {

View file

@ -1,7 +1,5 @@
use std::collections::HashSet;
use hir::{self, HasCrate, HasVisibility};
use ide_db::path_transform::PathTransform;
use ide_db::{path_transform::PathTransform, FxHashSet};
use syntax::{
ast::{
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<'
let sema_field_ty = ctx.sema.resolve_type(&field_ty)?;
let mut methods = vec![];
let mut seen_names = HashSet::new();
let mut seen_names = FxHashSet::default();
for ty in sema_field_ty.autoderef(ctx.db()) {
let krate = ty.krate(ctx.db());

View file

@ -502,9 +502,7 @@ fn generate_args_for_impl(
trait_params: &Option<GenericParamList>,
old_trait_args: &FxHashSet<String>,
) -> Option<ast::GenericArgList> {
let Some(old_impl_args) = old_impl_gpl.map(|gpl| gpl.to_generic_args().generic_args()) else {
return None;
};
let old_impl_args = old_impl_gpl.map(|gpl| gpl.to_generic_args().generic_args())?;
// Create pairs of the args of `self_ty` and corresponding `field_ty` to
// form the substitution list
let mut arg_substs = FxHashMap::default();

View file

@ -3,12 +3,12 @@
// - Remove unused aliases if there are no longer any users, see inline_call.rs.
use hir::{HasSource, PathResolution};
use ide_db::FxHashMap;
use ide_db::{
defs::Definition, imports::insert_use::ast_to_remove_for_path_in_use_stmt,
search::FileReference,
};
use itertools::Itertools;
use std::collections::HashMap;
use syntax::{
ast::{self, make, HasGenericParams, HasName},
ted, AstNode, NodeOrToken, SyntaxNode,
@ -189,14 +189,14 @@ fn inline(alias_def: &ast::TypeAlias, alias_instance: &ast::PathType) -> Option<
Some(repl)
}
struct LifetimeMap(HashMap<String, ast::Lifetime>);
struct LifetimeMap(FxHashMap<String, ast::Lifetime>);
impl LifetimeMap {
fn new(
instance_args: &Option<ast::GenericArgList>,
alias_generics: &ast::GenericParamList,
) -> Option<Self> {
let mut inner = HashMap::new();
let mut inner = FxHashMap::default();
let wildcard_lifetime = make::lifetime("'_");
let lifetimes = alias_generics
@ -231,14 +231,14 @@ impl LifetimeMap {
}
}
struct ConstAndTypeMap(HashMap<String, SyntaxNode>);
struct ConstAndTypeMap(FxHashMap<String, SyntaxNode>);
impl ConstAndTypeMap {
fn new(
instance_args: &Option<ast::GenericArgList>,
alias_generics: &ast::GenericParamList,
) -> Option<Self> {
let mut inner = HashMap::new();
let mut inner = FxHashMap::default();
let instance_generics = generic_args_to_const_and_type_generics(instance_args);
let alias_generics = generic_param_list_to_const_and_type_generics(alias_generics);

View file

@ -1,5 +1,6 @@
use hir::Type;
use std::{collections::HashMap, iter::successors};
use ide_db::FxHashMap;
use std::iter::successors;
use syntax::{
algo::neighbor,
ast::{self, AstNode, HasName},
@ -95,7 +96,7 @@ fn contains_placeholder(a: &ast::MatchArm) -> bool {
}
fn are_same_types(
current_arm_types: &HashMap<String, Option<Type>>,
current_arm_types: &FxHashMap<String, Option<Type>>,
arm: &ast::MatchArm,
ctx: &AssistContext<'_>,
) -> bool {
@ -114,11 +115,11 @@ fn are_same_types(
fn get_arm_types(
context: &AssistContext<'_>,
arm: &ast::MatchArm,
) -> HashMap<String, Option<Type>> {
let mut mapping: HashMap<String, Option<Type>> = HashMap::new();
) -> FxHashMap<String, Option<Type>> {
let mut mapping: FxHashMap<String, Option<Type>> = FxHashMap::default();
fn recurse(
map: &mut HashMap<String, Option<Type>>,
map: &mut FxHashMap<String, Option<Type>>,
ctx: &AssistContext<'_>,
pat: &Option<ast::Pat>,
) {

View file

@ -1,11 +1,11 @@
use std::collections::{hash_map::Entry, HashMap};
use std::collections::hash_map::Entry;
use hir::{HirFileIdExt, InFile, InRealFile, Module, ModuleSource};
use ide_db::{
base_db::FileRange,
defs::Definition,
search::{FileReference, ReferenceCategory, SearchScope},
RootDatabase,
FxHashMap, RootDatabase,
};
use syntax::{ast, AstNode};
use text_edit::TextRange;
@ -44,7 +44,7 @@ pub(crate) fn remove_unused_imports(acc: &mut Assists, ctx: &AssistContext<'_>)
let uses = uses_up.chain(uses_down).collect::<Vec<_>>();
// Maps use nodes to the scope that we should search through to find
let mut search_scopes = HashMap::<Module, Vec<SearchScope>>::new();
let mut search_scopes = FxHashMap::<Module, Vec<SearchScope>>::default();
// iterator over all unused use trees
let mut unused = uses

View file

@ -47,9 +47,7 @@ pub(crate) fn unwrap_result_return_type(acc: &mut Assists, ctx: &AssistContext<'
return None;
}
let Some(ok_type) = unwrap_result_type(type_ref) else {
return None;
};
let ok_type = unwrap_result_type(type_ref)?;
acc.add(
AssistId("unwrap_result_return_type", AssistKind::RefactorRewrite),

View file

@ -1,9 +1,7 @@
//! This module contains functions to suggest names for expressions, functions and other items
use std::collections::HashSet;
use hir::Semantics;
use ide_db::RootDatabase;
use ide_db::{FxHashSet, RootDatabase};
use itertools::Itertools;
use stdx::to_lower_snake_case;
use syntax::{
@ -78,7 +76,7 @@ pub(crate) fn for_unique_generic_name(
ast::GenericParam::TypeParam(t) => t.name().unwrap().to_string(),
p => p.to_string(),
})
.collect::<HashSet<_>>();
.collect::<FxHashSet<_>>();
let mut name = name.to_string();
let base_len = name.len();
let mut count = 0;

View file

@ -241,6 +241,7 @@ fn unescape(s: &str) -> String {
s.replace(r#"\""#, "").replace(r#"\n"#, "\n").replace(r#"\r"#, "")
}
#[allow(clippy::print_stderr)]
fn generate_descriptor_clippy(buf: &mut String, path: &Path) {
let file_content = std::fs::read_to_string(path).unwrap();
let mut clippy_lints: Vec<ClippyLint> = Vec::new();

View file

@ -73,8 +73,6 @@ mod handlers {
#[cfg(test)]
mod tests;
use std::collections::HashMap;
use hir::{diagnostics::AnyDiagnostic, InFile, Semantics};
use ide_db::{
assists::{Assist, AssistId, AssistKind, AssistResolveStrategy},
@ -413,18 +411,18 @@ pub fn diagnostics(
// `__RA_EVERY_LINT` is a fake lint group to allow every lint in proc macros
static RUSTC_LINT_GROUPS_DICT: Lazy<HashMap<&str, Vec<&str>>> =
static RUSTC_LINT_GROUPS_DICT: Lazy<FxHashMap<&str, Vec<&str>>> =
Lazy::new(|| build_group_dict(DEFAULT_LINT_GROUPS, &["warnings", "__RA_EVERY_LINT"], ""));
static CLIPPY_LINT_GROUPS_DICT: Lazy<HashMap<&str, Vec<&str>>> =
static CLIPPY_LINT_GROUPS_DICT: Lazy<FxHashMap<&str, Vec<&str>>> =
Lazy::new(|| build_group_dict(CLIPPY_LINT_GROUPS, &["__RA_EVERY_LINT"], "clippy::"));
fn build_group_dict(
lint_group: &'static [LintGroup],
all_groups: &'static [&'static str],
prefix: &'static str,
) -> HashMap<&'static str, Vec<&'static str>> {
let mut r: HashMap<&str, Vec<&str>> = HashMap::new();
) -> FxHashMap<&'static str, Vec<&'static str>> {
let mut r: FxHashMap<&str, Vec<&str>> = FxHashMap::default();
for g in lint_group {
for child in g.children {
r.entry(child.strip_prefix(prefix).unwrap())

View file

@ -1,3 +1,4 @@
#![allow(clippy::print_stderr)]
#[cfg(not(feature = "in-rust-tree"))]
mod sourcegen;

View file

@ -706,7 +706,7 @@ where
// we are trying to match that bit of code. This saves us having to pass a boolean into all the bits
// of code that can make the decision to not match.
thread_local! {
pub static RECORDING_MATCH_FAIL_REASONS: Cell<bool> = Cell::new(false);
pub static RECORDING_MATCH_FAIL_REASONS: Cell<bool> = const { Cell::new(false) };
}
fn recording_match_fail_reasons() -> bool {

View file

@ -113,6 +113,7 @@ fn assert_ssr_transforms(rules: &[&str], input: &str, expected: Expect) {
expected.assert_eq(&actual);
}
#[allow(clippy::print_stdout)]
fn print_match_debug_info(match_finder: &MatchFinder<'_>, file_id: FileId, snippet: &str) {
let debug_info = match_finder.debug_where_text_equal(file_id, snippet);
println!(

View file

@ -29,9 +29,6 @@ fn check_external_docs(
let web_url = links.web_url;
let local_url = links.local_url;
println!("web_url: {:?}", web_url);
println!("local_url: {:?}", local_url);
match (expect_web_url, web_url) {
(Some(expect), Some(url)) => expect.assert_eq(&url),
(None, None) => (),

View file

@ -9,8 +9,6 @@
//! at the index that the match starts at and its tree parent is
//! resolved to the search element definition, we get a reference.
use std::collections::HashMap;
use hir::{DescendPreference, PathResolution, Semantics};
use ide_db::{
base_db::FileId,
@ -79,7 +77,7 @@ pub(crate) fn find_all_refs(
.collect(),
)
})
.collect::<HashMap<_, Vec<_>, _>>();
.collect::<IntMap<_, Vec<_>>>();
let declaration = match def {
Definition::Module(module) => {
Some(NavigationTarget::from_module_to_decl(sema.db, module))

View file

@ -1,14 +1,12 @@
//! This module provides `StaticIndex` which is used for powering
//! read-only code browsers and emitting LSIF
use std::collections::HashMap;
use hir::{db::HirDatabase, Crate, HirFileIdExt, Module};
use ide_db::helpers::get_definition;
use ide_db::{
base_db::{FileId, FileRange, SourceDatabaseExt},
defs::Definition,
FxHashSet, RootDatabase,
helpers::get_definition,
FxHashMap, FxHashSet, RootDatabase,
};
use syntax::{AstNode, SyntaxKind::*, TextRange, T};
@ -31,7 +29,7 @@ pub struct StaticIndex<'a> {
pub tokens: TokenStore,
analysis: &'a Analysis,
db: &'a RootDatabase,
def_map: HashMap<Definition, TokenId>,
def_map: FxHashMap<Definition, TokenId>,
}
#[derive(Debug)]
@ -232,14 +230,13 @@ impl StaticIndex<'_> {
#[cfg(test)]
mod tests {
use crate::{fixture, StaticIndex};
use ide_db::base_db::FileRange;
use std::collections::HashSet;
use ide_db::{base_db::FileRange, FxHashSet};
use syntax::TextSize;
fn check_all_ranges(ra_fixture: &str) {
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
let s = StaticIndex::compute(&analysis);
let mut range_set: HashSet<_> = ranges.iter().map(|it| it.0).collect();
let mut range_set: FxHashSet<_> = ranges.iter().map(|it| it.0).collect();
for f in s.files {
for (range, _) in f.tokens {
let it = FileRange { file_id: f.file_id, range };
@ -258,7 +255,7 @@ mod tests {
fn check_definitions(ra_fixture: &str) {
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
let s = StaticIndex::compute(&analysis);
let mut range_set: HashSet<_> = ranges.iter().map(|it| it.0).collect();
let mut range_set: FxHashSet<_> = ranges.iter().map(|it| it.0).collect();
for (_, t) in s.tokens.iter() {
if let Some(t) = t.definition {
if t.range.start() == TextSize::from(0) {

View file

@ -55,13 +55,12 @@ impl Limit {
if other <= old_max || old_max == 0 {
break;
}
if self
.max
.compare_exchange_weak(old_max, other, Ordering::Relaxed, Ordering::Relaxed)
.is_ok()
{
eprintln!("new max: {other}");
}
_ = self.max.compare_exchange_weak(
old_max,
other,
Ordering::Relaxed,
Ordering::Relaxed,
);
}
Ok(())

View file

@ -1,5 +1,4 @@
use std::collections::HashMap;
use rustc_hash::FxHashMap;
use syntax::{ast, AstNode};
use test_utils::extract_annotations;
use tt::{
@ -12,7 +11,7 @@ use crate::{syntax_node_to_token_tree, DummyTestSpanData, DummyTestSpanMap, DUMM
fn check_punct_spacing(fixture: &str) {
let source_file = ast::SourceFile::parse(fixture).ok().unwrap();
let subtree = syntax_node_to_token_tree(source_file.syntax(), DummyTestSpanMap, DUMMY);
let mut annotations: HashMap<_, _> = extract_annotations(fixture)
let mut annotations: FxHashMap<_, _> = extract_annotations(fixture)
.into_iter()
.map(|(range, annotation)| {
let spacing = match annotation.as_str() {

View file

@ -1,5 +1,6 @@
//! This module greps parser's code for specially formatted comments and turns
//! them into tests.
#![allow(clippy::disallowed_types, clippy::print_stdout)]
use std::{
collections::HashMap,

View file

@ -23,6 +23,7 @@ serde.workspace = true
serde_json = { workspace = true, features = ["unbounded_depth"] }
tracing.workspace = true
triomphe.workspace = true
rustc-hash.workspace = true
memmap2 = "0.5.4"
snap = "1.1.0"
indexmap = "2.1.0"

View file

@ -35,10 +35,11 @@
//! as we don't have bincode in Cargo.toml yet, lets stick with serde_json for
//! the time being.
use std::collections::{HashMap, VecDeque};
use std::collections::VecDeque;
use indexmap::IndexSet;
use la_arena::RawIdx;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use span::{ErasedFileAstId, FileId, Span, SpanAnchor, SyntaxContextId};
use text_size::TextRange;
@ -129,7 +130,7 @@ impl FlatTree {
span_data_table: &mut SpanDataIndexMap,
) -> FlatTree {
let mut w = Writer {
string_table: HashMap::new(),
string_table: FxHashMap::default(),
work: VecDeque::new(),
span_data_table,
@ -158,7 +159,7 @@ impl FlatTree {
pub fn new_raw(subtree: &tt::Subtree<TokenId>, version: u32) -> FlatTree {
let mut w = Writer {
string_table: HashMap::new(),
string_table: FxHashMap::default(),
work: VecDeque::new(),
span_data_table: &mut (),
@ -340,7 +341,7 @@ impl InternableSpan for Span {
struct Writer<'a, 'span, S: InternableSpan> {
work: VecDeque<(usize, &'a tt::Subtree<S>)>,
string_table: HashMap<&'a str, u32>,
string_table: FxHashMap<&'a str, u32>,
span_data_table: &'span mut S::Table,
subtree: Vec<SubtreeRepr>,

View file

@ -1,6 +1,8 @@
//! A standalone binary for `proc-macro-srv`.
//! Driver for proc macro server
#![cfg_attr(feature = "in-rust-tree", feature(rustc_private))]
#![allow(clippy::print_stderr)]
#[cfg(feature = "in-rust-tree")]
extern crate rustc_driver as _;

View file

@ -2,6 +2,7 @@
#![warn(rust_2018_idioms, unused_lifetimes)]
#![feature(proc_macro_span, proc_macro_def_site)]
#![allow(clippy::all)]
use proc_macro::{Group, Ident, Literal, Punct, Span, TokenStream, TokenTree};

View file

@ -21,7 +21,7 @@ pub use countme;
/// almost zero.
pub use countme::Count;
thread_local!(static IN_SCOPE: RefCell<bool> = RefCell::new(false));
thread_local!(static IN_SCOPE: RefCell<bool> = const { RefCell::new(false) });
/// Allows to check if the current code is within some dynamic scope, can be
/// useful during debugging to figure out why a function is called.
@ -88,6 +88,7 @@ pub fn cpu_span() -> CpuSpan {
}
#[cfg(not(feature = "cpu_profiler"))]
#[allow(clippy::print_stderr)]
{
eprintln!(
r#"cpu profiling is disabled, uncomment `default = [ "cpu_profiler" ]` in Cargo.toml to enable."#

View file

@ -1,4 +1,7 @@
//! Like `std::time::Instant`, but also measures memory & CPU cycles.
#![allow(clippy::print_stderr)]
use std::{
fmt,
time::{Duration, Instant},

View file

@ -399,7 +399,7 @@ impl CargoWorkspace {
CargoWorkspace { packages, targets, workspace_root, target_directory }
}
pub fn packages(&self) -> impl Iterator<Item = Package> + ExactSizeIterator + '_ {
pub fn packages(&self) -> impl ExactSizeIterator<Item = Package> + '_ {
self.packages.iter().map(|(id, _pkg)| id)
}

View file

@ -57,7 +57,7 @@ impl Stitched {
self.by_name("proc_macro")
}
pub(crate) fn crates(&self) -> impl Iterator<Item = SysrootCrate> + ExactSizeIterator + '_ {
pub(crate) fn crates(&self) -> impl ExactSizeIterator<Item = SysrootCrate> + '_ {
self.crates.iter().map(|(id, _data)| id)
}

View file

@ -3,7 +3,9 @@
//! Based on cli flags, either spawns an LSP server, or runs a batch analysis
#![warn(rust_2018_idioms, unused_lifetimes)]
#![allow(clippy::print_stdout, clippy::print_stderr)]
#![cfg_attr(feature = "in-rust-tree", feature(rustc_private))]
#[cfg(feature = "in-rust-tree")]
extern crate rustc_driver as _;

View file

@ -1,5 +1,7 @@
//! Various batch processing tasks, intended primarily for debugging.
#![allow(clippy::print_stdout, clippy::print_stderr)]
mod analysis_stats;
mod diagnostics;
pub mod flags;

View file

@ -1,6 +1,5 @@
//! LSIF (language server index format) generator
use std::collections::HashMap;
use std::env;
use std::time::Instant;
@ -16,6 +15,7 @@ use ide_db::{
use load_cargo::{load_workspace, LoadCargoConfig, ProcMacroServerChoice};
use lsp_types::{self, lsif};
use project_model::{CargoConfig, ProjectManifest, ProjectWorkspace, RustLibSource};
use rustc_hash::FxHashMap;
use vfs::{AbsPathBuf, Vfs};
use crate::{
@ -35,10 +35,10 @@ impl<DB: ParallelDatabase> Clone for Snap<salsa::Snapshot<DB>> {
struct LsifManager<'a> {
count: i32,
token_map: HashMap<TokenId, Id>,
range_map: HashMap<FileRange, Id>,
file_map: HashMap<FileId, Id>,
package_map: HashMap<PackageInformation, Id>,
token_map: FxHashMap<TokenId, Id>,
range_map: FxHashMap<FileRange, Id>,
file_map: FxHashMap<FileId, Id>,
package_map: FxHashMap<PackageInformation, Id>,
analysis: &'a Analysis,
db: &'a RootDatabase,
vfs: &'a Vfs,
@ -57,10 +57,10 @@ impl LsifManager<'_> {
fn new<'a>(analysis: &'a Analysis, db: &'a RootDatabase, vfs: &'a Vfs) -> LsifManager<'a> {
LsifManager {
count: 0,
token_map: HashMap::default(),
range_map: HashMap::default(),
file_map: HashMap::default(),
package_map: HashMap::default(),
token_map: FxHashMap::default(),
range_map: FxHashMap::default(),
file_map: FxHashMap::default(),
package_map: FxHashMap::default(),
analysis,
db,
vfs,
@ -215,7 +215,7 @@ impl LsifManager<'_> {
out_v: result_set_id.into(),
}));
let mut edges = token.references.iter().fold(
HashMap::<_, Vec<lsp_types::NumberOrString>>::new(),
FxHashMap::<_, Vec<lsp_types::NumberOrString>>::default(),
|mut edges, it| {
let entry = edges.entry((it.range.file_id, it.is_definition)).or_default();
entry.push((*self.range_map.get(&it.range).unwrap()).into());

View file

@ -1,8 +1,6 @@
//! Run all tests in a project, similar to `cargo test`, but using the mir interpreter.
use std::{
cell::RefCell, collections::HashMap, fs::read_to_string, panic::AssertUnwindSafe, path::PathBuf,
};
use std::{cell::RefCell, fs::read_to_string, panic::AssertUnwindSafe, path::PathBuf};
use hir::{Change, Crate};
use ide::{AnalysisHost, DiagnosticCode, DiagnosticsConfig};
@ -10,6 +8,7 @@ use profile::StopWatch;
use project_model::{CargoConfig, ProjectWorkspace, RustLibSource, Sysroot};
use load_cargo::{load_workspace, LoadCargoConfig, ProcMacroServerChoice};
use rustc_hash::FxHashMap;
use triomphe::Arc;
use vfs::{AbsPathBuf, FileId};
use walkdir::WalkDir;
@ -27,7 +26,7 @@ struct Tester {
fn string_to_diagnostic_code_leaky(code: &str) -> DiagnosticCode {
thread_local! {
static LEAK_STORE: RefCell<HashMap<String, DiagnosticCode>> = RefCell::new(HashMap::new());
static LEAK_STORE: RefCell<FxHashMap<String, DiagnosticCode>> = RefCell::new(FxHashMap::default());
}
LEAK_STORE.with_borrow_mut(|s| match s.get(code) {
Some(c) => *c,
@ -39,9 +38,9 @@ fn string_to_diagnostic_code_leaky(code: &str) -> DiagnosticCode {
})
}
fn detect_errors_from_rustc_stderr_file(p: PathBuf) -> HashMap<DiagnosticCode, usize> {
fn detect_errors_from_rustc_stderr_file(p: PathBuf) -> FxHashMap<DiagnosticCode, usize> {
let text = read_to_string(p).unwrap();
let mut result = HashMap::new();
let mut result = FxHashMap::default();
{
let mut text = &*text;
while let Some(p) = text.find("error[E") {
@ -106,7 +105,7 @@ impl Tester {
let expected = if stderr_path.exists() {
detect_errors_from_rustc_stderr_file(stderr_path)
} else {
HashMap::new()
FxHashMap::default()
};
let text = read_to_string(&p).unwrap();
let mut change = Change::new();
@ -125,7 +124,7 @@ impl Tester {
self.host.apply_change(change);
let diagnostic_config = DiagnosticsConfig::test_sample();
let mut actual = HashMap::new();
let mut actual = FxHashMap::default();
let panicked = match std::panic::catch_unwind(|| {
self.host
.analysis()

View file

@ -1,10 +1,6 @@
//! SCIP generator
use std::{
collections::{HashMap, HashSet},
path::PathBuf,
time::Instant,
};
use std::{path::PathBuf, time::Instant};
use ide::{
LineCol, MonikerDescriptorKind, MonikerResult, StaticIndex, StaticIndexedFile,
@ -12,6 +8,7 @@ use ide::{
};
use ide_db::LineIndexDatabase;
use load_cargo::{load_workspace_at, LoadCargoConfig, ProcMacroServerChoice};
use rustc_hash::{FxHashMap, FxHashSet};
use scip::types as scip_types;
use crate::{
@ -76,9 +73,10 @@ impl flags::Scip {
};
let mut documents = Vec::new();
let mut symbols_emitted: HashSet<TokenId> = HashSet::default();
let mut tokens_to_symbol: HashMap<TokenId, String> = HashMap::new();
let mut tokens_to_enclosing_symbol: HashMap<TokenId, Option<String>> = HashMap::new();
let mut symbols_emitted: FxHashSet<TokenId> = FxHashSet::default();
let mut tokens_to_symbol: FxHashMap<TokenId, String> = FxHashMap::default();
let mut tokens_to_enclosing_symbol: FxHashMap<TokenId, Option<String>> =
FxHashMap::default();
for StaticIndexedFile { file_id, tokens, .. } in si.files {
let mut local_count = 0;

View file

@ -1,9 +1,9 @@
//! This module provides the functionality needed to convert diagnostics from
//! `cargo check` json format to the LSP diagnostic format.
use std::collections::HashMap;
use flycheck::{Applicability, DiagnosticLevel, DiagnosticSpan};
use itertools::Itertools;
use rustc_hash::FxHashMap;
use stdx::format_to;
use vfs::{AbsPath, AbsPathBuf};
@ -186,7 +186,7 @@ fn map_rust_child_diagnostic(
return MappedRustChildDiagnostic::MessageLine(rd.message.clone());
}
let mut edit_map: HashMap<lsp_types::Url, Vec<lsp_types::TextEdit>> = HashMap::new();
let mut edit_map: FxHashMap<lsp_types::Url, Vec<lsp_types::TextEdit>> = FxHashMap::default();
let mut suggested_replacements = Vec::new();
let mut is_preferred = true;
for &span in &spans {

View file

@ -1,6 +1,8 @@
//! rust-analyzer extensions to the LSP.
use std::{collections::HashMap, path::PathBuf};
#![allow(clippy::disallowed_types)]
use std::path::PathBuf;
use ide_db::line_index::WideEncoding;
use lsp_types::request::Request;
@ -9,6 +11,7 @@ use lsp_types::{
PartialResultParams, Position, Range, TextDocumentIdentifier, WorkDoneProgressParams,
};
use lsp_types::{PositionEncodingKind, Url};
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use crate::line_index::PositionEncoding;
@ -448,12 +451,16 @@ pub struct CodeActionData {
#[serde(rename_all = "camelCase")]
pub struct SnippetWorkspaceEdit {
#[serde(skip_serializing_if = "Option::is_none")]
pub changes: Option<HashMap<lsp_types::Url, Vec<lsp_types::TextEdit>>>,
pub changes: Option<FxHashMap<lsp_types::Url, Vec<lsp_types::TextEdit>>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub document_changes: Option<Vec<SnippetDocumentChangeOperation>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub change_annotations:
Option<HashMap<lsp_types::ChangeAnnotationIdentifier, lsp_types::ChangeAnnotation>>,
pub change_annotations: Option<
std::collections::HashMap<
lsp_types::ChangeAnnotationIdentifier,
lsp_types::ChangeAnnotation,
>,
>,
}
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]

View file

@ -179,6 +179,7 @@ impl Node {
self.go(0, filter)
}
#[allow(clippy::print_stderr)]
fn go(&self, level: usize, filter: &WriteFilter) {
if self.duration > filter.longer_than && level < filter.depth {
let duration = ms(self.duration);

View file

@ -9,6 +9,7 @@
//! be sure without a real client anyway.
#![warn(rust_2018_idioms, unused_lifetimes)]
#![allow(clippy::disallowed_types)]
#[cfg(not(feature = "in-rust-tree"))]
mod sourcegen;

View file

@ -1,3 +1,4 @@
#![allow(clippy::disallowed_types, clippy::print_stderr)]
use std::{
collections::HashSet,
path::{Path, PathBuf},
@ -78,8 +79,6 @@ fn files_are_tidy() {
match extension {
"rs" => {
let text = sh.read_file(&path).unwrap();
check_todo(&path, &text);
check_dbg(&path, &text);
check_test_attrs(&path, &text);
check_trailing_ws(&path, &text);
tidy_docs.visit(&path, &text);
@ -205,74 +204,6 @@ Zlib OR Apache-2.0 OR MIT
assert_eq!(licenses, expected);
}
fn check_todo(path: &Path, text: &str) {
let need_todo = &[
// This file itself obviously needs to use todo (<- like this!).
"tests/tidy.rs",
// Some of our assists generate `todo!()`.
"handlers/add_turbo_fish.rs",
"handlers/generate_function.rs",
"handlers/add_missing_match_arms.rs",
"handlers/replace_derive_with_manual_impl.rs",
// To support generating `todo!()` in assists, we have `expr_todo()` in
// `ast::make`.
"ast/make.rs",
// The documentation in string literals may contain anything for its own purposes
"ide-db/src/generated/lints.rs",
"ide-assists/src/utils/gen_trait_fn_body.rs",
"ide-assists/src/tests/generated.rs",
// The tests for missing fields
"ide-diagnostics/src/handlers/missing_fields.rs",
];
if need_todo.iter().any(|p| path.ends_with(p)) {
return;
}
if text.contains("TODO") || text.contains("TOOD") || text.contains("todo!") {
// Generated by an assist
if text.contains("${0:todo!()}") {
return;
}
panic!(
"\nTODO markers or todo! macros should not be committed to the master branch,\n\
use FIXME instead\n\
{}\n",
path.display(),
)
}
}
fn check_dbg(path: &Path, text: &str) {
let need_dbg = &[
// This file itself obviously needs to use dbg.
"slow-tests/tidy.rs",
// Assists to remove `dbg!()`
"handlers/remove_dbg.rs",
// We have .dbg postfix
"ide-completion/src/completions/postfix.rs",
"ide-completion/src/completions/keyword.rs",
"ide-completion/src/tests/expression.rs",
"ide-completion/src/tests/proc_macros.rs",
// The documentation in string literals may contain anything for its own purposes
"ide-completion/src/lib.rs",
"ide-db/src/generated/lints.rs",
// test for doc test for remove_dbg
"src/tests/generated.rs",
// `expect!` string can contain `dbg!` (due to .dbg postfix)
"ide-completion/src/tests/special.rs",
];
if need_dbg.iter().any(|p| path.ends_with(p)) {
return;
}
if text.contains("dbg!") {
panic!(
"\ndbg! macros should not be committed to the master branch,\n\
{}\n",
path.display(),
)
}
}
fn check_test_attrs(path: &Path, text: &str) {
let ignore_rule =
"https://github.com/rust-lang/rust-analyzer/blob/master/docs/dev/style.md#ignore";

View file

@ -167,6 +167,7 @@ pub fn add_preamble(generator: &'static str, mut text: String) -> String {
/// Checks that the `file` has the specified `contents`. If that is not the
/// case, updates the file and then fails the test.
#[allow(clippy::print_stderr)]
pub fn ensure_file_contents(file: &Path, contents: &str) {
if let Ok(old_contents) = fs::read_to_string(file) {
if normalize_newlines(&old_contents) == normalize_newlines(contents) {

View file

@ -54,12 +54,13 @@ use core::any::{Any, TypeId};
use core::hash::BuildHasherDefault;
use core::marker::PhantomData;
use ::std::collections::hash_map::{self, HashMap};
use ::std::collections::hash_map;
/// Raw access to the underlying `HashMap`.
///
/// This alias is provided for convenience because of the ugly third generic parameter.
pub type RawMap<A> = HashMap<TypeId, Box<A>, BuildHasherDefault<TypeIdHasher>>;
#[allow(clippy::disallowed_types)] // Uses a custom hasher
pub type RawMap<A> = hash_map::HashMap<TypeId, Box<A>, BuildHasherDefault<TypeIdHasher>>;
/// A collection containing zero or one values for any given type and allowing convenient,
/// type-safe access to those values.

View file

@ -23,12 +23,14 @@ pub fn is_ci() -> bool {
}
#[must_use]
#[allow(clippy::print_stderr)]
pub fn timeit(label: &'static str) -> impl Drop {
let start = Instant::now();
defer(move || eprintln!("{}: {:.2?}", label, start.elapsed()))
}
/// Prints backtrace to stderr, useful for debugging.
#[allow(clippy::print_stderr)]
pub fn print_backtrace() {
#[cfg(feature = "backtrace")]
eprintln!("{:?}", backtrace::Backtrace::new());

View file

@ -18,6 +18,7 @@ pub struct PanicContext {
}
impl PanicContext {
#[allow(clippy::print_stderr)]
fn init() {
let default_hook = panic::take_hook();
let hook = move |panic_info: &panic::PanicInfo<'_>| {
@ -43,7 +44,7 @@ impl Drop for PanicContext {
fn with_ctx(f: impl FnOnce(&mut Vec<String>)) {
thread_local! {
static CTX: RefCell<Vec<String>> = RefCell::new(Vec::new());
static CTX: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
}
CTX.with(|ctx| f(&mut ctx.borrow_mut()));
}

View file

@ -14,8 +14,7 @@ pub fn shuffle<T>(slice: &mut [T], mut rand_index: impl FnMut(usize) -> usize) {
}
pub fn seed() -> u64 {
use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hasher};
RandomState::new().build_hasher().finish()
#[allow(clippy::disallowed_types)]
std::collections::hash_map::RandomState::new().build_hasher().finish()
}

View file

@ -46,6 +46,7 @@ impl CheckReparse {
Some(CheckReparse { text, edit, edited_text })
}
#[allow(clippy::print_stderr)]
pub fn run(&self) {
let parse = SourceFile::parse(&self.text);
let new_parse = parse.reparse(&self.edit);

View file

@ -3,14 +3,12 @@
//! Specifically, it generates the `SyntaxKind` enum and a number of newtype
//! wrappers around `SyntaxNode` which implement `syntax::AstNode`.
use std::{
collections::{BTreeSet, HashSet},
fmt::Write,
};
use std::{collections::BTreeSet, fmt::Write};
use itertools::Itertools;
use proc_macro2::{Punct, Spacing};
use quote::{format_ident, quote};
use rustc_hash::FxHashSet;
use ungrammar::{Grammar, Rule};
use crate::tests::ast_src::{
@ -278,7 +276,7 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> String {
}
});
let defined_nodes: HashSet<_> = node_names.collect();
let defined_nodes: FxHashSet<_> = node_names.collect();
for node in kinds
.nodes

View file

@ -7,6 +7,7 @@
//! * marks (see the eponymous module).
#![warn(rust_2018_idioms, unused_lifetimes)]
#![allow(clippy::print_stderr)]
mod assert_linear;
pub mod bench_fixture;
@ -424,7 +425,7 @@ pub fn format_diff(chunks: Vec<dissimilar::Chunk<'_>>) -> String {
///
/// A benchmark test looks like this:
///
/// ```
/// ```ignore
/// #[test]
/// fn benchmark_foo() {
/// if skip_slow_tests() { return; }

View file

@ -1,5 +1,5 @@
<!---
lsp/ext.rs hash: dff0b009e82ef06a
lsp/ext.rs hash: c0bf3acd7a9e72b2
If you need to change the above hash to make the test pass, please check if you
need to adjust this doc as well and ping this issue:

View file

@ -374,7 +374,7 @@ impl<T> Arena<T> {
/// ```
pub fn iter(
&self,
) -> impl Iterator<Item = (Idx<T>, &T)> + ExactSizeIterator + DoubleEndedIterator + Clone {
) -> impl ExactSizeIterator<Item = (Idx<T>, &T)> + DoubleEndedIterator + Clone {
self.data.iter().enumerate().map(|(idx, value)| (Idx::from_raw(RawIdx(idx as u32)), value))
}
@ -394,7 +394,7 @@ impl<T> Arena<T> {
/// ```
pub fn iter_mut(
&mut self,
) -> impl Iterator<Item = (Idx<T>, &mut T)> + ExactSizeIterator + DoubleEndedIterator {
) -> impl ExactSizeIterator<Item = (Idx<T>, &mut T)> + DoubleEndedIterator {
self.data
.iter_mut()
.enumerate()
@ -414,7 +414,7 @@ impl<T> Arena<T> {
/// assert_eq!(iterator.next(), Some(&40));
/// assert_eq!(iterator.next(), Some(&60));
/// ```
pub fn values(&self) -> impl Iterator<Item = &T> + ExactSizeIterator + DoubleEndedIterator {
pub fn values(&self) -> impl ExactSizeIterator<Item = &T> + DoubleEndedIterator {
self.data.iter()
}
@ -432,9 +432,7 @@ impl<T> Arena<T> {
///
/// assert_eq!(arena[idx1], 10);
/// ```
pub fn values_mut(
&mut self,
) -> impl Iterator<Item = &mut T> + ExactSizeIterator + DoubleEndedIterator {
pub fn values_mut(&mut self) -> impl ExactSizeIterator<Item = &mut T> + DoubleEndedIterator {
self.data.iter_mut()
}

View file

@ -73,17 +73,17 @@ impl<T, V> ArenaMap<Idx<T>, V> {
}
/// Returns an iterator over the values in the map.
pub fn values(&self) -> impl Iterator<Item = &V> + DoubleEndedIterator {
pub fn values(&self) -> impl DoubleEndedIterator<Item = &V> {
self.v.iter().filter_map(|o| o.as_ref())
}
/// Returns an iterator over mutable references to the values in the map.
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V> + DoubleEndedIterator {
pub fn values_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut V> {
self.v.iter_mut().filter_map(|o| o.as_mut())
}
/// Returns an iterator over the arena indexes and values in the map.
pub fn iter(&self) -> impl Iterator<Item = (Idx<T>, &V)> + DoubleEndedIterator {
pub fn iter(&self) -> impl DoubleEndedIterator<Item = (Idx<T>, &V)> {
self.v.iter().enumerate().filter_map(|(idx, o)| Some((Self::from_idx(idx), o.as_ref()?)))
}

View file

@ -41,6 +41,9 @@
//!
//! {"jsonrpc": "2.0", "method": "exit", "params": null}
//! ```
#![allow(clippy::print_stderr)]
use std::error::Error;
use lsp_types::OneOf;

View file

@ -5,6 +5,7 @@
//! Run with `RUST_LOG=lsp_server=debug` to see all the messages.
#![warn(rust_2018_idioms, unused_lifetimes)]
#![allow(clippy::print_stdout, clippy::disallowed_types)]
mod error;
mod msg;

View file

@ -9,6 +9,7 @@
//! `.cargo/config`.
#![warn(rust_2018_idioms, unused_lifetimes)]
#![allow(clippy::print_stderr, clippy::print_stdout)]
mod flags;