From c532b4a099dd50f468a267f4eafa47ca4291ac9d Mon Sep 17 00:00:00 2001 From: Hong Jiarong Date: Sat, 15 Nov 2025 21:56:08 +0800 Subject: [PATCH] simplify dead code checks and enhance diagnostic messages for unused symbols --- crates/tinymist-lint/src/dead_code.rs | 6 +---- .../tinymist-lint/src/dead_code/diagnostic.rs | 25 ++++++++++++------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/crates/tinymist-lint/src/dead_code.rs b/crates/tinymist-lint/src/dead_code.rs index cfdc124d..5e537197 100644 --- a/crates/tinymist-lint/src/dead_code.rs +++ b/crates/tinymist-lint/src/dead_code.rs @@ -84,11 +84,7 @@ fn should_skip_definition(def_info: &DefInfo, config: &DeadCodeConfig) -> bool { matches!( def_info.decl.as_ref(), - Decl::ModuleImport(_) - | Decl::Pattern(_) - | Decl::Spread(_) - | Decl::Constant(_) - | Decl::Content(_) + Decl::Pattern(_) | Decl::Spread(_) | Decl::Constant(_) | Decl::Content(_) ) } diff --git a/crates/tinymist-lint/src/dead_code/diagnostic.rs b/crates/tinymist-lint/src/dead_code/diagnostic.rs index 337f43dc..1fb7b34a 100644 --- a/crates/tinymist-lint/src/dead_code/diagnostic.rs +++ b/crates/tinymist-lint/src/dead_code/diagnostic.rs @@ -3,7 +3,7 @@ //! This module creates user-friendly diagnostic messages for unused //! definitions, with appropriate hints and severity levels. -use tinymist_analysis::syntax::{DefKind, ExprInfo}; +use tinymist_analysis::syntax::{Decl, DefKind, ExprInfo}; use tinymist_project::LspWorld; use typst::diag::{SourceDiagnostic, eco_format}; @@ -23,6 +23,9 @@ pub fn generate_diagnostic( return None; } + let is_module_import = matches!(def_info.decl.as_ref(), Decl::ModuleImport(..)); + let is_module_like = is_module_import || matches!(def_info.kind, DefKind::Module); + let kind_str = match def_info.kind { DefKind::Function => "function", DefKind::Variable => "variable", @@ -35,13 +38,16 @@ pub fn generate_diagnostic( let name = def_info.decl.name(); // Don't warn about empty names (anonymous items) - if name.is_empty() { + if name.is_empty() && !is_module_import { return None; } // Create the base diagnostic - let mut diag = - SourceDiagnostic::warning(def_info.span, eco_format!("unused {kind_str}: `{name}`")); + let mut diag = if is_module_import { + SourceDiagnostic::warning(def_info.span, eco_format!("unused module import")) + } else { + SourceDiagnostic::warning(def_info.span, eco_format!("unused {kind_str}: `{name}`")) + }; // Add helpful hints based on the scope and kind match def_info.scope { @@ -50,11 +56,12 @@ pub fn generate_diagnostic( "if this parameter is intentionally unused, prefix it with underscore: `_{name}`" )); } - DefScope::File | DefScope::Local => { + DefScope::File | DefScope::Local if !is_module_like => { diag = diag.with_hint(eco_format!( "consider removing this {kind_str} or prefixing with underscore: `_{name}`" )); } + DefScope::File | DefScope::Local => {} DefScope::Exported => { diag = diag.with_hint(eco_format!("this {kind_str} is exported but never used")); } @@ -71,12 +78,12 @@ pub fn generate_diagnostic( return None; } } - DefKind::Module => { - diag = - diag.with_hint("imported modules should be used or the import should be removed"); - } _ => {} } + if is_module_like { + diag = diag.with_hint("imported modules should be used or the import should be removed"); + } + Some(diag) }