Skip to content

Commit 849f23d

Browse files
committed
clear many unwrap
1 parent eebd3d8 commit 849f23d

File tree

17 files changed

+245
-97
lines changed

17 files changed

+245
-97
lines changed

crates/emmylua_code_analysis/src/compilation/analyzer/bind_type/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn bind_type(
2020
.bind_type(type_owner.clone(), type_cache);
2121
migrate_global_members_when_type_resolve(db, type_owner);
2222
} else {
23-
let decl_type = decl_type_cache.unwrap().as_type();
23+
let decl_type = decl_type_cache?.as_type();
2424
merge_def_type(db, decl_type.clone(), type_cache.as_type().clone());
2525
}
2626

crates/emmylua_code_analysis/src/compilation/analyzer/doc/file_generic_index.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ impl FileGenericIndex {
5959
id: GenericParamId,
6060
effect_id: GenericEffectId,
6161
) -> bool {
62-
let effect_node = self.effect_nodes.get(effect_id.id).unwrap();
62+
let effect_node = match self.effect_nodes.get(effect_id.id) {
63+
Some(node) => node,
64+
None => return false,
65+
};
6366

6467
if effect_node.range.contains_range(range) {
6568
let children = effect_node.children.clone();
@@ -77,7 +80,10 @@ impl FileGenericIndex {
7780

7881
let child_node_id = self.effect_nodes.len();
7982
self.effect_nodes.push(child_node);
80-
let effect_node = self.effect_nodes.get_mut(effect_id.id).unwrap();
83+
let effect_node = match self.effect_nodes.get_mut(effect_id.id) {
84+
Some(node) => node,
85+
None => return false,
86+
};
8187
effect_node
8288
.children
8389
.push(GenericEffectId::new(child_node_id));

crates/emmylua_code_analysis/src/compilation/analyzer/lua/stats.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ pub fn analyze_assign_stat(analyzer: &mut LuaAnalyzer, assign_stat: LuaAssignSta
252252
if expr.is_none() {
253253
break;
254254
}
255-
let expr = expr.unwrap();
255+
let expr = expr?;
256256
let type_owner = get_var_owner(analyzer, var.clone());
257257
set_index_expr_owner(analyzer, var.clone());
258258

crates/emmylua_code_analysis/src/compilation/analyzer/mod.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,17 @@ fn module_analyze(
3838
let in_filed_tree = need_analyzed_files[0].clone();
3939
let file_id = in_filed_tree.file_id;
4040
if let Some(path) = db.get_vfs().get_file_path(&file_id).cloned() {
41+
let path_str = match path.to_str() {
42+
Some(path) => path,
43+
None => {
44+
log::warn!("file_id {:?} path not found", file_id);
45+
return vec![];
46+
}
47+
};
48+
4149
let workspace_id = db
4250
.get_module_index_mut()
43-
.add_module_by_path(file_id, path.to_str().unwrap());
51+
.add_module_by_path(file_id, path_str);
4452
let workspace_id = workspace_id.unwrap_or(WorkspaceId::MAIN);
4553
let mut context = AnalyzeContext::new(config);
4654
context.add_tree_chunk(in_filed_tree);
@@ -55,9 +63,17 @@ fn module_analyze(
5563
for in_filed_tree in need_analyzed_files {
5664
let file_id = in_filed_tree.file_id;
5765
if let Some(path) = db.get_vfs().get_file_path(&file_id).cloned() {
66+
let path_str = match path.to_str() {
67+
Some(path) => path,
68+
None => {
69+
log::warn!("file_id {:?} path not found", file_id);
70+
continue;
71+
}
72+
};
73+
5874
let workspace_id = db
5975
.get_module_index_mut()
60-
.add_module_by_path(file_id, path.to_str().unwrap());
76+
.add_module_by_path(file_id, path_str);
6177
let workspace_id = workspace_id.unwrap_or(WorkspaceId::MAIN);
6278
file_tree_map
6379
.entry(workspace_id)

crates/emmylua_code_analysis/src/compilation/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ impl LuaCompilation {
3939
pub fn update_index(&mut self, file_ids: Vec<FileId>) {
4040
let mut need_analyzed_files = vec![];
4141
for file_id in file_ids {
42-
let tree = self.db.get_vfs().get_syntax_tree(&file_id).unwrap();
42+
let tree = match self.db.get_vfs().get_syntax_tree(&file_id) {
43+
Some(tree) => tree,
44+
None => {
45+
log::warn!("file_id {:?} not found in vfs", file_id);
46+
continue;
47+
}
48+
};
4349
need_analyzed_files.push(InFiled {
4450
file_id,
4551
value: tree.get_chunk_node(),

crates/emmylua_code_analysis/src/config/config_loader.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ pub fn load_configs(config_files: Vec<PathBuf>, partial_emmyrcs: Option<Vec<Valu
4343
log::info!("No valid config file found.");
4444
Emmyrc::default()
4545
} else if config_jsons.len() == 1 {
46-
let first_config = config_jsons.into_iter().next().unwrap();
46+
let first_config = match config_jsons.into_iter().next() {
47+
Some(config) => config,
48+
None => {
49+
log::error!("No valid config file found.");
50+
return Emmyrc::default();
51+
}
52+
};
53+
4754
let flatten_config = FlattenConfigObject::parse(first_config);
4855
let emmyrc_json_value = flatten_config.to_emmyrc();
4956
let emmyrc: Emmyrc = match serde_json::from_value(emmyrc_json_value) {

crates/emmylua_code_analysis/src/config/mod.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,24 @@ fn pre_process_path(path: &str, workspace: &Path) -> String {
106106
path = replace_env_var(&path);
107107
// ${workspaceFolder} == {workspaceFolder}
108108
path = path.replace("$", "");
109-
path = replace_placeholders(&path, workspace.to_str().unwrap());
109+
let workspace_str = match workspace.to_str() {
110+
Some(path) => path,
111+
None => {
112+
log::error!("Warning: workspace path is not valid UTF-8");
113+
return path;
114+
}
115+
};
116+
117+
path = replace_placeholders(&path, workspace_str);
110118

111119
if path.starts_with('~') {
112-
let home_dir = dirs::home_dir().unwrap();
120+
let home_dir = match dirs::home_dir() {
121+
Some(path) => path,
122+
None => {
123+
log::error!("Warning: Home directory not found");
124+
return path;
125+
}
126+
};
113127
path = home_dir.join(&path[1..]).to_string_lossy().to_string();
114128
} else if path.starts_with("./") {
115129
path = workspace.join(&path[2..]).to_string_lossy().to_string();
@@ -124,7 +138,13 @@ fn pre_process_path(path: &str, workspace: &Path) -> String {
124138

125139
// compact luals
126140
fn replace_env_var(path: &str) -> String {
127-
let re = Regex::new(r"\$(\w+)").unwrap();
141+
let re = match Regex::new(r"\$(\w+)") {
142+
Ok(re) => re,
143+
Err(_) => {
144+
log::error!("Warning: Failed to create regex for environment variable replacement");
145+
return path.to_string();
146+
}
147+
};
128148
re.replace_all(path, |caps: &regex::Captures| {
129149
let key = &caps[1];
130150
std::env::var(key).unwrap_or_else(|_| {
@@ -136,7 +156,13 @@ fn replace_env_var(path: &str) -> String {
136156
}
137157

138158
fn replace_placeholders(input: &str, workspace_folder: &str) -> String {
139-
let re = Regex::new(r"\{([^}]+)\}").unwrap();
159+
let re = match Regex::new(r"\{([^}]+)\}") {
160+
Ok(re) => re,
161+
Err(_) => {
162+
log::error!("Warning: Failed to create regex for placeholder replacement");
163+
return input.to_string();
164+
}
165+
};
140166
re.replace_all(input, |caps: &regex::Captures| {
141167
let key = &caps[1];
142168
if key == "workspaceFolder" {

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

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,21 @@ impl LuaDeclarationTree {
3535
self.walk_up(scope, position, 0, &mut |decl_id| {
3636
match decl_id {
3737
ScopeOrDeclId::Decl(decl_id) => {
38-
let decl = self.get_decl(&decl_id).unwrap();
38+
let decl = match self.get_decl(&decl_id) {
39+
Some(decl) => decl,
40+
None => return false,
41+
};
3942
if decl.get_name() == name {
4043
result = Some(decl);
4144
return true;
4245
}
4346
}
4447
ScopeOrDeclId::Scope(scope) => {
45-
let scope = self.scopes.get(scope.id as usize).unwrap();
48+
let scope = match self.scopes.get(scope.id as usize) {
49+
Some(scope) => scope,
50+
None => return false,
51+
};
52+
4653
// self found method stat, donot return decl id
4754
if scope.get_kind() == LuaScopeKind::MethodStat && name == "self" {
4855
return true;
@@ -75,23 +82,24 @@ impl LuaDeclarationTree {
7582
if self.scopes.is_empty() {
7683
return None;
7784
}
78-
let mut scope = self.scopes.get(0).unwrap();
85+
let mut scope = match self.scopes.get(0) {
86+
Some(scope) => scope,
87+
None => return None,
88+
};
89+
7990
loop {
8091
let child_scope = scope
8192
.get_children()
8293
.iter()
8394
.filter_map(|child| match child {
84-
ScopeOrDeclId::Scope(child_id) => {
85-
let child_scope = self.scopes.get(child_id.id as usize).unwrap();
86-
Some(child_scope)
87-
}
95+
ScopeOrDeclId::Scope(child_id) => self.scopes.get(child_id.id as usize),
8896
ScopeOrDeclId::Decl(_) => None,
8997
})
9098
.find(|child_scope| child_scope.get_range().contains(position));
9199
if child_scope.is_none() {
92100
break;
93101
}
94-
scope = child_scope.unwrap();
102+
scope = child_scope?;
95103
}
96104

97105
Some(scope)
@@ -104,21 +112,33 @@ impl LuaDeclarationTree {
104112
let cur_index = scope.get_children().iter().rposition(|child| match child {
105113
ScopeOrDeclId::Decl(decl_id) => decl_id.position < start_pos,
106114
ScopeOrDeclId::Scope(scope_id) => {
107-
let child_scope = self.scopes.get(scope_id.id as usize).unwrap();
115+
let child_scope = match self.scopes.get(scope_id.id as usize) {
116+
Some(scope) => scope,
117+
None => return false,
118+
};
108119
child_scope.get_position() < start_pos
109120
}
110121
});
111122

112123
if let Some(cur_index) = cur_index {
113124
for i in (0..=cur_index).rev() {
114-
match scope.get_children().get(i).unwrap() {
125+
let scope_or_id = match scope.get_children().get(i) {
126+
Some(scope_or_id) => scope_or_id,
127+
None => continue,
128+
};
129+
130+
match scope_or_id {
115131
ScopeOrDeclId::Decl(decl_id) => {
116132
if f(decl_id.into()) {
117133
return;
118134
}
119135
}
120136
ScopeOrDeclId::Scope(scope_id) => {
121-
let child_scope = self.scopes.get(scope_id.id as usize).unwrap();
137+
let child_scope = match self.scopes.get(scope_id.id as usize) {
138+
Some(scope) => scope,
139+
None => continue,
140+
};
141+
122142
if self.walk_over_scope(child_scope, f) {
123143
return;
124144
}
@@ -128,7 +148,10 @@ impl LuaDeclarationTree {
128148
}
129149

130150
if let Some(parent_id) = scope.get_parent() {
131-
let parent_scope = self.scopes.get(parent_id.id as usize).unwrap();
151+
let parent_scope = match self.scopes.get(parent_id.id as usize) {
152+
Some(scope) => scope,
153+
None => return,
154+
};
132155
self.walk_up(parent_scope, start_pos, level + 1, f);
133156
}
134157
}
@@ -142,14 +165,20 @@ impl LuaDeclarationTree {
142165
LuaScopeKind::LocalOrAssignStat => {
143166
let parent = scope.get_parent();
144167
if let Some(parent) = parent {
145-
let parent_scope = self.scopes.get(parent.id as usize).unwrap();
168+
let parent_scope = match self.scopes.get(parent.id as usize) {
169+
Some(scope) => scope,
170+
None => return,
171+
};
146172
self.walk_up(parent_scope, scope.get_position(), level, f);
147173
}
148174
}
149175
LuaScopeKind::Repeat => {
150176
if level == 0 {
151177
if let Some(ScopeOrDeclId::Scope(scope_id)) = scope.get_children().get(0) {
152-
let scope = self.scopes.get(scope_id.id as usize).unwrap();
178+
let scope = match self.scopes.get(scope_id.id as usize) {
179+
Some(scope) => scope,
180+
None => return,
181+
};
153182
self.walk_up(scope, start_pos, level, f);
154183
return;
155184
}
@@ -161,7 +190,10 @@ impl LuaDeclarationTree {
161190
if level == 0 {
162191
let parent = scope.get_parent();
163192
if let Some(parent) = parent {
164-
let parent_scope = self.scopes.get(parent.id as usize).unwrap();
193+
let parent_scope = match self.scopes.get(parent.id as usize) {
194+
Some(scope) => scope,
195+
None => return,
196+
};
165197
self.walk_up(parent_scope, start_pos, level, f);
166198
}
167199
} else {
@@ -249,7 +281,10 @@ impl LuaDeclarationTree {
249281
self.walk_up(scope, position, 0, &mut |decl_id| {
250282
match decl_id {
251283
ScopeOrDeclId::Decl(decl_id) => {
252-
let decl = self.get_decl(&decl_id).unwrap();
284+
let decl = match self.get_decl(&decl_id) {
285+
Some(decl) => decl,
286+
None => return false,
287+
};
253288
if decl.get_name() == "self" {
254289
result = Some(LuaDeclOrMemberId::Decl(decl_id));
255290
return true;

0 commit comments

Comments
 (0)