Skip to content

Commit 74eb947

Browse files
committed
add local const check
1 parent 2cf9bcb commit 74eb947

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
use crate::{DiagnosticCode, LocalAttribute, LuaDecl, LuaDeclId, SemanticModel};
3+
4+
use super::DiagnosticContext;
5+
6+
pub const CODES: &[DiagnosticCode] = &[DiagnosticCode::LocalConstReassign];
7+
8+
pub fn check(context: &mut DiagnosticContext, semantic_model: &SemanticModel) -> Option<()> {
9+
let file_id = semantic_model.get_file_id();
10+
let decl_tree = semantic_model
11+
.get_db()
12+
.get_decl_index()
13+
.get_decl_tree(&file_id)?;
14+
for (decl_id, decl) in decl_tree.get_decls() {
15+
match decl {
16+
LuaDecl::Local { attrib, .. } => {
17+
if let Some(attrib) = attrib {
18+
if matches!(attrib, LocalAttribute::Const | LocalAttribute::IterConst) {
19+
check_local_const_reassign(context, semantic_model, decl_id);
20+
}
21+
}
22+
}
23+
_ => {}
24+
}
25+
}
26+
27+
Some(())
28+
}
29+
30+
fn check_local_const_reassign(
31+
context: &mut DiagnosticContext,
32+
semantic_model: &SemanticModel,
33+
decl_id: &LuaDeclId,
34+
) -> Option<()> {
35+
let file_id = semantic_model.get_file_id();
36+
let refs_index = semantic_model.get_db().get_reference_index();
37+
let local_refs = refs_index.get_local_reference(&file_id)?;
38+
let ranges = local_refs.get_local_references(decl_id)?;
39+
for range in ranges {
40+
if refs_index.is_write_range(file_id, *range) {
41+
context.add_diagnostic(
42+
DiagnosticCode::LocalConstReassign,
43+
*range,
44+
t!("Cannot reassign to a constant variable").to_string(),
45+
None,
46+
);
47+
}
48+
}
49+
50+
Some(())
51+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod deprecated;
55
mod undefined_global;
66
mod access_invisible;
77
mod missing_parameter;
8+
mod local_const_reassign;
89

910
use lsp_types::{Diagnostic, DiagnosticSeverity, DiagnosticTag, NumberOrString};
1011
use rowan::TextRange;
@@ -36,6 +37,7 @@ pub fn check_file(context: &mut DiagnosticContext, semantic_model:&mut SemanticM
3637
check!(undefined_global);
3738
check!(access_invisible);
3839
check!(missing_parameter);
40+
check!(local_const_reassign);
3941

4042
Some(())
4143
}

0 commit comments

Comments
 (0)