diff --git a/lsp/src/main.rs b/lsp/src/main.rs index 0411ba96..5dc4f795 100644 --- a/lsp/src/main.rs +++ b/lsp/src/main.rs @@ -46,7 +46,7 @@ impl Backend { code: None, code_description: None, source: Some("Enderpy".to_string()), - message: String::from(err.msg), + message: err.msg, related_information: None, tags: None, data: None, diff --git a/parser/src/parser/parser.rs b/parser/src/parser/parser.rs index fafb88da..89cb4973 100644 --- a/parser/src/parser/parser.rs +++ b/parser/src/parser/parser.rs @@ -179,12 +179,12 @@ impl Parser { self.bump_any(); let range = self.finish_node(node); let line_number = self.get_line_number_of_character_position(range.start); - return Err(miette!( + Err(miette!( "Unexpected token {:?} at line {} at position {}", kind, line_number, range.start, - )); + )) } fn get_line_number_of_character_position(&self, pos: usize) -> u32 { @@ -1522,18 +1522,14 @@ impl Parser { } let started_with_star = self.at(Kind::Mul); let first_elm = self.parse_star_named_expression()?; - if !started_with_star && self.at(Kind::For) { - if self.at(Kind::For) - || self.at(Kind::Async) && matches!(self.peek_kind(), Ok(Kind::For)) - { - let generators = self.parse_comp_for()?; - self.expect(Kind::RightBrace)?; - return Ok(Expression::ListComp(Box::new(ListComp { - node: self.finish_node(node), - element: Box::new(first_elm), - generators, - }))); - } + if !started_with_star && self.at(Kind::For) && (self.at(Kind::For) || self.at(Kind::Async) && matches!(self.peek_kind(), Ok(Kind::For))) { + let generators = self.parse_comp_for()?; + self.expect(Kind::RightBrace)?; + return Ok(Expression::ListComp(Box::new(ListComp { + node: self.finish_node(node), + element: Box::new(first_elm), + generators, + }))); } self.bump(Kind::Comma); let rest = self.parse_starred_list(Kind::RightBrace)?; @@ -1764,19 +1760,15 @@ impl Parser { // https://docs.python.org/3/reference/expressions.html#set-displays fn parse_set(&mut self, node: Node, first_elm: Expression) -> Result { - if !matches!(first_elm, Expression::Starred(_)) && self.at(Kind::For) { - if self.at(Kind::For) - || self.at(Kind::Async) && matches!(self.peek_kind(), Ok(Kind::For)) - { - let generators = self.parse_comp_for()?; - self.consume_whitespace_and_newline(); - self.expect(Kind::RightBracket)?; - return Ok(Expression::SetComp(Box::new(SetComp { - node: self.finish_node(node), - element: Box::new(first_elm), - generators, - }))); - } + if !matches!(first_elm, Expression::Starred(_)) && self.at(Kind::For) && (self.at(Kind::For) || self.at(Kind::Async) && matches!(self.peek_kind(), Ok(Kind::For))) { + let generators = self.parse_comp_for()?; + self.consume_whitespace_and_newline(); + self.expect(Kind::RightBracket)?; + return Ok(Expression::SetComp(Box::new(SetComp { + node: self.finish_node(node), + element: Box::new(first_elm), + generators, + }))); } self.bump(Kind::Comma); let rest = self.parse_starred_list(Kind::RightBracket)?; @@ -1796,12 +1788,12 @@ impl Parser { if self.at(Kind::For) || self.at(Kind::Async) && matches!(self.peek_kind(), Ok(Kind::For)) { let generators = self.parse_comp_for()?; self.expect(Kind::RightBracket)?; - return Ok(Expression::DictComp(Box::new(DictComp { + Ok(Expression::DictComp(Box::new(DictComp { node: self.finish_node(node), key: Box::new(first_key), value: Box::new(first_val), generators, - }))); + }))) } else { // we already consumed the first pair // so if there are more pairs we need to consume the comma @@ -1836,7 +1828,7 @@ impl Parser { self.bump(self.cur_kind()); consumed = true; } - return consumed; + consumed } // https://docs.python.org/3/reference/expressions.html#expression-lists diff --git a/parser/src/token.rs b/parser/src/token.rs index 986ffd38..eeef338e 100644 --- a/parser/src/token.rs +++ b/parser/src/token.rs @@ -16,7 +16,7 @@ impl Display for Token { let start = self.start.to_string(); let end = self.end.to_string(); let mut s = format!("{},{}-{}:{} ", start, end, start, end); - s.push_str(&kind); + s.push_str(kind); let mut padding = 50 - s.len(); while padding > 0 { diff --git a/typechecker/src/ast_visitor.rs b/typechecker/src/ast_visitor.rs index 4e6dbb7a..3a4e4ffb 100644 --- a/typechecker/src/ast_visitor.rs +++ b/typechecker/src/ast_visitor.rs @@ -196,13 +196,13 @@ pub trait TraversalVisitor { fn visit_match_pattern(&mut self, _m: &parser::ast::MatchPattern) { match _m { MatchPattern::MatchValue(m) => self.visit_expr(&m.value), - MatchPattern::MatchSingleton(m) => self.visit_expr(&m), + MatchPattern::MatchSingleton(m) => self.visit_expr(m), MatchPattern::MatchSequence(m) => { for item in m.iter() { self.visit_match_pattern(item); } }, - MatchPattern::MatchStar(m) => self.visit_expr(&m), + MatchPattern::MatchStar(m) => self.visit_expr(m), MatchPattern::MatchMapping(m) => { for key in &m.keys { self.visit_expr(key); diff --git a/typechecker/src/build.rs b/typechecker/src/build.rs index f2ae0496..14e331ed 100644 --- a/typechecker/src/build.rs +++ b/typechecker/src/build.rs @@ -91,8 +91,7 @@ impl BuildManager { pub fn get_module_name(path: &PathBuf) -> String { path.to_str() .unwrap_or_default() - .replace("/", ".") - .replace("\\", ".") + .replace(['/', '\\'], ".") } // Entry point to analyze the program @@ -170,7 +169,7 @@ impl BuildManager { discovered_files.extend(next_imports.clone()); new_imports = next_imports; } - return discovered_files; + discovered_files } // Resolves imports in a file and return the resolved paths @@ -254,7 +253,7 @@ impl BuildManager { } } - return resolved_paths; + resolved_paths } } diff --git a/typechecker/src/ruff_python_import_resolver/resolver.rs b/typechecker/src/ruff_python_import_resolver/resolver.rs index 11355b99..0fc0b1e2 100644 --- a/typechecker/src/ruff_python_import_resolver/resolver.rs +++ b/typechecker/src/ruff_python_import_resolver/resolver.rs @@ -381,13 +381,10 @@ fn resolve_best_absolute_import( "Looking in typeshed root directory: {}", typeshed_root.display() ); - if typeshed_root != execution_environment.root { - if best_result_so_far + if typeshed_root != execution_environment.root && best_result_so_far .as_ref() - .is_some_and(|result| result.py_typed_info.is_some() && !result.is_partly_resolved) - { - return best_result_so_far; - } + .is_some_and(|result| result.py_typed_info.is_some() && !result.is_partly_resolved) { + return best_result_so_far; } } @@ -442,12 +439,10 @@ fn find_typeshed_path( if let Some(path) = search::stdlib_typeshed_path(config, host) { typeshed_paths.push(path); } - } else { - if let Some(paths) = - search::third_party_typeshed_package_paths(module_descriptor, config, host) - { - typeshed_paths.extend(paths); - } + } else if let Some(paths) = + search::third_party_typeshed_package_paths(module_descriptor, config, host) + { + typeshed_paths.extend(paths); } for typeshed_path in typeshed_paths { @@ -519,28 +514,23 @@ fn pick_best_import( // If both results are namespace imports, prefer the result that resolves all // imported symbols. - if best_import_so_far.is_namespace_package && new_import.is_namespace_package { - if !module_descriptor.imported_symbols.is_empty() { - if !best_import_so_far + if best_import_so_far.is_namespace_package && new_import.is_namespace_package && !module_descriptor.imported_symbols.is_empty() && !best_import_so_far .implicit_imports - .resolves_namespace_package(&module_descriptor.imported_symbols) - { - if new_import - .implicit_imports - .resolves_namespace_package(&module_descriptor.imported_symbols) - { - return new_import; - } + .resolves_namespace_package(&module_descriptor.imported_symbols) { + if new_import + .implicit_imports + .resolves_namespace_package(&module_descriptor.imported_symbols) + { + return new_import; + } - // Prefer the namespace package that has an `__init__.py[i]` file present in the - // final directory over one that does not. - if best_import_so_far.is_init_file_present && !new_import.is_init_file_present { - return best_import_so_far; - } - if !best_import_so_far.is_init_file_present && new_import.is_init_file_present { - return new_import; - } - } + // Prefer the namespace package that has an `__init__.py[i]` file present in the + // final directory over one that does not. + if best_import_so_far.is_init_file_present && !new_import.is_init_file_present { + return best_import_so_far; + } + if !best_import_so_far.is_init_file_present && new_import.is_init_file_present { + return new_import; } } diff --git a/typechecker/src/ruff_python_import_resolver/search.rs b/typechecker/src/ruff_python_import_resolver/search.rs index e5a53ff4..00e92652 100644 --- a/typechecker/src/ruff_python_import_resolver/search.rs +++ b/typechecker/src/ruff_python_import_resolver/search.rs @@ -51,22 +51,16 @@ fn find_site_packages_path( if dir_path .file_name() .and_then(OsStr::to_str)? - .starts_with("python3.") - { - if dir_path.join(SITE_PACKAGES).is_dir() { - return Some(dir_path); - } + .starts_with("python3.") && dir_path.join(SITE_PACKAGES).is_dir() { + return Some(dir_path); } } else if metadata.file_type().is_symlink() { let symlink_path = fs::read_link(entry.path()).ok()?; if symlink_path .file_name() .and_then(OsStr::to_str)? - .starts_with("python3.") - { - if symlink_path.join(SITE_PACKAGES).is_dir() { - return Some(symlink_path); - } + .starts_with("python3.") && symlink_path.join(SITE_PACKAGES).is_dir() { + return Some(symlink_path); } } @@ -230,23 +224,20 @@ fn build_typeshed_third_party_package_map( .entry(inner_entry.file_name().to_string_lossy().to_string()) .or_insert_with(Vec::new) .push(outer_entry.path()); - } else if inner_entry.file_type()?.is_file() { - if inner_entry + } else if inner_entry.file_type()?.is_file() && inner_entry .path() .extension() - .is_some_and(|extension| extension == "pyi") + .is_some_and(|extension| extension == "pyi") { + if let Some(stripped_file_name) = inner_entry + .path() + .file_stem() + .and_then(std::ffi::OsStr::to_str) + .map(std::string::ToString::to_string) { - if let Some(stripped_file_name) = inner_entry - .path() - .file_stem() - .and_then(std::ffi::OsStr::to_str) - .map(std::string::ToString::to_string) - { - package_map - .entry(stripped_file_name) - .or_insert_with(Vec::new) - .push(outer_entry.path()); - } + package_map + .entry(stripped_file_name) + .or_insert_with(Vec::new) + .push(outer_entry.path()); } } } diff --git a/typechecker/src/symbol_table.rs b/typechecker/src/symbol_table.rs index 090e5047..c58767cc 100644 --- a/typechecker/src/symbol_table.rs +++ b/typechecker/src/symbol_table.rs @@ -198,7 +198,7 @@ impl SymbolTableNode { .cmp(&b.declaration_path().node.start) }); - filtered_declarations.last().map(|decl| *decl) + filtered_declarations.last().copied() } } diff --git a/typechecker/src/type_check/checker.rs b/typechecker/src/type_check/checker.rs index 5baa5108..f4f67301 100644 --- a/typechecker/src/type_check/checker.rs +++ b/typechecker/src/type_check/checker.rs @@ -266,13 +266,13 @@ impl<'a> TraversalVisitor for TypeChecker<'a> { fn visit_match_pattern(&mut self, _m: &parser::ast::MatchPattern) { match _m { MatchPattern::MatchValue(m) => self.visit_expr(&m.value), - MatchPattern::MatchSingleton(m) => self.visit_expr(&m), + MatchPattern::MatchSingleton(m) => self.visit_expr(m), MatchPattern::MatchSequence(m) => { for item in m.iter() { self.visit_match_pattern(item); } } - MatchPattern::MatchStar(m) => self.visit_expr(&m), + MatchPattern::MatchStar(m) => self.visit_expr(m), MatchPattern::MatchMapping(m) => { for key in &m.keys { self.visit_expr(key); @@ -457,7 +457,7 @@ impl<'a> TraversalVisitor for TypeChecker<'a> { fn visit_compare(&mut self, _c: &Compare) { self.visit_expr(&_c.left); for comprators in &_c.comparators { - self.visit_expr(&comprators); + self.visit_expr(comprators); } } diff --git a/typechecker/src/type_check/rules.rs b/typechecker/src/type_check/rules.rs index 7f361f7f..a50f5d62 100644 --- a/typechecker/src/type_check/rules.rs +++ b/typechecker/src/type_check/rules.rs @@ -8,5 +8,5 @@ pub fn is_reassignment_valid(old_type: &PythonType, new_type: &PythonType) -> bo return true; } - return false; + false } diff --git a/typechecker/src/type_check/type_evaluator.rs b/typechecker/src/type_check/type_evaluator.rs index ad44f76e..0bff7bb1 100755 --- a/typechecker/src/type_check/type_evaluator.rs +++ b/typechecker/src/type_check/type_evaluator.rs @@ -35,9 +35,9 @@ impl TypeEvaluator { let decl = symbol .declaration_until_position(position) .ok_or_else(|| miette!("symbol {} is not defined", symbol.name))?; - return self - .get_type_from_declaration(&decl) - .map_err(|e| miette!("cannot infer type for symbol {}: {}", symbol.name, e)); + self + .get_type_from_declaration(decl) + .map_err(|e| miette!("cannot infer type for symbol {}: {}", symbol.name, e)) } pub fn get_type(&self, expr: &ast::Expression) -> Result { match expr { @@ -190,7 +190,9 @@ impl TypeEvaluator { } fn get_type_from_declaration(&self, declaration: &Declaration) -> Result { - let decl_type = match declaration { + + + match declaration { Declaration::Variable(v) => { if let Some(type_annotation) = &v.type_annotation { Ok(type_inference::get_type_from_annotation(type_annotation)) @@ -219,9 +221,7 @@ impl TypeEvaluator { } Declaration::Class(_) => Ok(PythonType::Unknown), Declaration::Parameter(_) => Ok(PythonType::Unknown), - }; - - decl_type + } } fn infer_type_from_symbol_table(&self, name: &str, position: usize) -> Result { @@ -577,7 +577,7 @@ mod tests { let mut result_sorted = result.clone().into_iter().collect::>(); result_sorted.sort_by(|a, b| a.0.cmp(&b.0)); - return format!("{:#?}", result_sorted); + format!("{:#?}", result_sorted) } macro_rules! snap_type_eval {