Skip to content

Commit

Permalink
Fix exhaustives
Browse files Browse the repository at this point in the history
  • Loading branch information
Kampfkarren committed Jul 6, 2024
1 parent 18f23bb commit 9a5979c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
21 changes: 12 additions & 9 deletions selene-lib/src/lints/deprecated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,18 @@ impl Visitor for DeprecatedVisitor<'_> {
let call_suffix = suffixes.pop().unwrap();

let function_args = match call_suffix {
#[cfg_attr(
feature = "force_exhaustive_checks",
deny(non_exhaustive_omitted_patterns)
)]
ast::Suffix::Call(call) => match call {
ast::Call::AnonymousCall(args) => args,
ast::Call::MethodCall(method_call) => method_call.args(),
_ => return,
},
ast::Suffix::Call(call) =>
{
#[cfg_attr(
feature = "force_exhaustive_checks",
deny(non_exhaustive_omitted_patterns)
)]
match call {
ast::Call::AnonymousCall(args) => args,
ast::Call::MethodCall(method_call) => method_call.args(),
_ => return,
}
}

_ => unreachable!("function_call.call_suffix != ast::Suffix::Call"),
};
Expand Down
48 changes: 25 additions & 23 deletions selene-lib/src/lints/standard_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
use std::convert::Infallible;

use full_moon::{
ast::{self, Ast},
ast::{self, Ast, Expression},
node::Node,
tokenizer::{Position, Symbol, TokenType},
visitors::Visitor,
Expand Down Expand Up @@ -39,6 +39,17 @@ impl Lint for StandardLibraryLint {
}
}

fn same_type_if_equal(lhs: &Expression, rhs: &Expression) -> Option<PassedArgumentType> {
let lhs_type = get_argument_type(lhs);
let rhs_type = get_argument_type(rhs);

if lhs_type == rhs_type {
lhs_type
} else {
None
}
}

// Returns the argument type of the expression if it can be constantly resolved
// Otherwise, returns None
// Only attempts to resolve constants
Expand All @@ -57,6 +68,8 @@ fn get_argument_type(expression: &ast::Expression) -> Option<PassedArgumentType>
ast::UnOp::Hash(_) => Some(ArgumentType::Number.into()),
ast::UnOp::Minus(_) => get_argument_type(expression),
ast::UnOp::Not(_) => Some(ArgumentType::Bool.into()),
#[cfg(feature = "lua53")]
ast::UnOp::Tilde(_) => get_argument_type(expression),
_ => None,
}
}
Expand Down Expand Up @@ -155,16 +168,17 @@ fn get_argument_type(expression: &ast::Expression) -> Option<PassedArgumentType>
ast::BinOp::Plus(_)
| ast::BinOp::Minus(_)
| ast::BinOp::Star(_)
| ast::BinOp::Slash(_) => {
let lhs_type = get_argument_type(lhs);
let rhs_type = get_argument_type(rhs);

if lhs_type == rhs_type {
lhs_type
} else {
None
}
}
| ast::BinOp::Slash(_) => same_type_if_equal(lhs, rhs),

#[cfg(feature = "lua53")]
ast::BinOp::DoubleLessThan(_)
| ast::BinOp::DoubleGreaterThan(_)
| ast::BinOp::Ampersand(_)
| ast::BinOp::Tilde(_)
| ast::BinOp::Pipe(_) => same_type_if_equal(lhs, rhs),

#[cfg(any(feature = "lua53", feature = "roblox"))]
ast::BinOp::DoubleSlash(_) => same_type_if_equal(lhs, rhs),

ast::BinOp::Percent(_) => Some(ArgumentType::Number.into()),

Expand All @@ -177,18 +191,6 @@ fn get_argument_type(expression: &ast::Expression) -> Option<PassedArgumentType>
None
}

#[cfg(feature = "roblox")]
ast::BinOp::DoubleSlash(_) => {
let lhs_type = get_argument_type(lhs);
let rhs_type = get_argument_type(rhs);

if lhs_type == rhs_type {
lhs_type
} else {
None
}
}

_ => None,
}
}
Expand Down

0 comments on commit 9a5979c

Please sign in to comment.