Skip to content

Commit

Permalink
Cargo clippy fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Sep 28, 2023
1 parent 55f328c commit 9e35d4b
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 107 deletions.
2 changes: 1 addition & 1 deletion lsp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
52 changes: 22 additions & 30 deletions parser/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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<Expression> {
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)?;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion parser/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions typechecker/src/ast_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions typechecker/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -254,7 +253,7 @@ impl BuildManager {
}
}

return resolved_paths;
resolved_paths
}
}

Expand Down
54 changes: 22 additions & 32 deletions typechecker/src/ruff_python_import_resolver/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,10 @@ fn resolve_best_absolute_import<Host: host::Host>(
"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;
}
}

Expand Down Expand Up @@ -442,12 +439,10 @@ fn find_typeshed_path<Host: host::Host>(
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 {
Expand Down Expand Up @@ -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;
}
}

Expand Down
39 changes: 15 additions & 24 deletions typechecker/src/ruff_python_import_resolver/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion typechecker/src/symbol_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl SymbolTableNode {
.cmp(&b.declaration_path().node.start)
});

filtered_declarations.last().map(|decl| *decl)
filtered_declarations.last().copied()
}
}

Expand Down
6 changes: 3 additions & 3 deletions typechecker/src/type_check/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion typechecker/src/type_check/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ pub fn is_reassignment_valid(old_type: &PythonType, new_type: &PythonType) -> bo
return true;
}

return false;
false
}
16 changes: 8 additions & 8 deletions typechecker/src/type_check/type_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PythonType> {
match expr {
Expand Down Expand Up @@ -190,7 +190,9 @@ impl TypeEvaluator {
}

fn get_type_from_declaration(&self, declaration: &Declaration) -> Result<PythonType> {
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))
Expand Down Expand Up @@ -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<PythonType> {
Expand Down Expand Up @@ -577,7 +577,7 @@ mod tests {
let mut result_sorted = result.clone().into_iter().collect::<Vec<_>>();
result_sorted.sort_by(|a, b| a.0.cmp(&b.0));

return format!("{:#?}", result_sorted);
format!("{:#?}", result_sorted)
}

macro_rules! snap_type_eval {
Expand Down

0 comments on commit 9e35d4b

Please sign in to comment.