|
| 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 | +} |
0 commit comments