Skip to content

Commit 24f1ece

Browse files
committed
add unused check
1 parent 7534ebc commit 24f1ece

File tree

7 files changed

+76
-16
lines changed

7 files changed

+76
-16
lines changed

crates/code_analysis/src/db_index/declaration/decl_tree.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ impl LuaDeclarationTree {
312312
pub fn get_scope(&self, scope_id: &LuaScopeId) -> Option<&LuaScope> {
313313
self.scopes.get(scope_id.id as usize)
314314
}
315+
316+
pub fn get_decls(&self) -> &HashMap<LuaDeclId, LuaDecl> {
317+
&self.decls
318+
}
315319
}
316320

317321
#[derive(Debug)]

crates/code_analysis/src/diagnostic/checker/analyze_error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use crate::DiagnosticCode;
1+
use crate::{DiagnosticCode, SemanticModel};
22

33
use super::DiagnosticContext;
44

55
pub const CODES: &[DiagnosticCode] = &[DiagnosticCode::TypeNotFound, DiagnosticCode::DuplicateType];
66

7-
pub fn check(context: &mut DiagnosticContext) -> Option<()> {
7+
pub fn check(context: &mut DiagnosticContext, _: &SemanticModel) -> Option<()> {
88
let db = context.get_db();
99
let file_id = context.get_file_id();
1010
let diagnostic_index = db.get_diagnostic_index();

crates/code_analysis/src/diagnostic/checker/depreacated.rs

Whitespace-only changes.

crates/code_analysis/src/diagnostic/checker/mod.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
mod analyze_error;
22
mod syntax_error;
3+
mod unused;
4+
mod depreacated;
35

46
use lsp_types::{Diagnostic, DiagnosticSeverity, DiagnosticTag, NumberOrString};
57
use rowan::TextRange;
@@ -12,51 +14,55 @@ use super::{
1214
DiagnosticCode,
1315
};
1416

15-
pub fn check_file(context: &mut DiagnosticContext) -> Option<()> {
17+
pub fn check_file(context: &mut DiagnosticContext, semantic_model:&mut SemanticModel) -> Option<()> {
1618
macro_rules! check {
1719
($module:ident) => {
1820
if $module::CODES
1921
.iter()
2022
.any(|code| context.is_checker_enable_by_code(code))
2123
{
22-
$module::check(context);
24+
$module::check(context, semantic_model);
2325
}
2426
};
2527
}
2628

2729
check!(syntax_error);
2830
check!(analyze_error);
31+
check!(unused);
2932

3033
Some(())
3134
}
3235

3336
pub struct DiagnosticContext<'a> {
34-
semantic_model: SemanticModel<'a>,
37+
file_id: FileId,
38+
db: &'a DbIndex,
3539
diagnostics: Vec<Diagnostic>,
3640
workspace_enabled: Arc<HashSet<DiagnosticCode>>,
3741
workspace_disabled: Arc<HashSet<DiagnosticCode>>,
3842
}
3943

4044
impl<'a> DiagnosticContext<'a> {
4145
pub fn new(
42-
semantic_model: SemanticModel<'a>,
46+
file_id: FileId,
47+
db: &'a DbIndex,
4348
workspace_enabled: Arc<HashSet<DiagnosticCode>>,
4449
workspace_disabled: Arc<HashSet<DiagnosticCode>>,
4550
) -> Self {
4651
Self {
47-
semantic_model,
52+
file_id,
53+
db,
4854
diagnostics: Vec::new(),
4955
workspace_disabled,
5056
workspace_enabled,
5157
}
5258
}
5359

5460
pub fn get_db(&self) -> &DbIndex {
55-
&self.semantic_model.get_db()
61+
&self.db
5662
}
5763

5864
pub fn get_file_id(&self) -> FileId {
59-
self.semantic_model.get_file_id()
65+
self.file_id
6066
}
6167

6268
pub fn add_diagnostic(
@@ -118,7 +124,7 @@ impl<'a> DiagnosticContext<'a> {
118124
}
119125

120126
fn translate_range(&self, range: TextRange) -> Option<lsp_types::Range> {
121-
let document = self.semantic_model.get_document();
127+
let document = self.db.get_vfs().get_document(&self.file_id)?;
122128
let (start_line, start_character) = document.get_line_col(range.start())?;
123129
let (end_line, end_character) = document.get_line_col(range.end())?;
124130

crates/code_analysis/src/diagnostic/checker/syntax_error.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use crate::DiagnosticCode;
1+
use crate::{DiagnosticCode, SemanticModel};
22

33
use super::DiagnosticContext;
44

55
pub const CODES: &[DiagnosticCode] = &[DiagnosticCode::SyntaxError];
66

7-
pub fn check(context: &mut DiagnosticContext) -> Option<()> {
8-
let semantic_model = &context.semantic_model;
7+
pub fn check(context: &mut DiagnosticContext, semantic_model: &SemanticModel) -> Option<()> {
98
if let Some(parse_errors) = semantic_model.get_file_parse_error() {
109
for parse_error in parse_errors {
1110
context.add_diagnostic(
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use crate::{DiagnosticCode, LuaDecl, LuaReferenceIndex, SemanticModel};
2+
3+
use super::DiagnosticContext;
4+
5+
pub const CODES: &[DiagnosticCode] = &[DiagnosticCode::Unused];
6+
7+
pub fn check(context: &mut DiagnosticContext, semantic_model: &SemanticModel) -> Option<()> {
8+
let file_id = semantic_model.get_file_id();
9+
let decl_tree = semantic_model
10+
.get_db()
11+
.get_decl_index()
12+
.get_decl_tree(&file_id)?;
13+
14+
let ref_index = semantic_model.get_db().get_reference_index();
15+
for (_, decl) in decl_tree.get_decls().iter() {
16+
if !is_decl_used(decl, ref_index) {
17+
let name = decl.get_name();
18+
if name.starts_with('_') {
19+
continue;
20+
}
21+
context.add_diagnostic(
22+
DiagnosticCode::Unused,
23+
decl.get_range(),
24+
format!(
25+
"{0} is never used, if this is intentional, prefix it with an underscore: _{0}",
26+
name
27+
),
28+
None,
29+
);
30+
}
31+
}
32+
33+
Some(())
34+
}
35+
36+
fn is_decl_used(decl: &LuaDecl, local_refs: &LuaReferenceIndex) -> bool {
37+
if decl.is_global() {
38+
return true;
39+
} else if decl.is_param() && decl.get_name() == "..." {
40+
return true;
41+
}
42+
43+
let file_id = decl.get_file_id();
44+
if let Some(refs) = local_refs.get_local_references(&file_id, &decl.get_id()) {
45+
return !refs.is_empty();
46+
}
47+
48+
false
49+
}

crates/code_analysis/src/diagnostic/lua_diagnostic.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,16 @@ impl LuaDiagnostic {
4242
return None;
4343
}
4444

45-
let model = compilation.get_semantic_model(file_id)?;
45+
let db = compilation.get_db();
46+
let mut semantic_model = compilation.get_semantic_model(file_id)?;
4647
let mut context = DiagnosticContext::new(
47-
model,
48+
file_id,
49+
db,
4850
self.workspace_enabled.clone(),
4951
self.workspace_disabled.clone(),
5052
);
5153

52-
check_file(&mut context);
54+
check_file(&mut context, &mut semantic_model);
5355

5456
Some(context.get_diagnostics())
5557
}

0 commit comments

Comments
 (0)