From 9a5979c961ce1b6ed3a16575e3d381f39e13f231 Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Sat, 6 Jul 2024 15:18:09 -0700 Subject: [PATCH] Fix exhaustives --- selene-lib/src/lints/deprecated.rs | 21 ++++++----- selene-lib/src/lints/standard_library.rs | 48 ++++++++++++------------ 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/selene-lib/src/lints/deprecated.rs b/selene-lib/src/lints/deprecated.rs index 262557d3..86658c38 100644 --- a/selene-lib/src/lints/deprecated.rs +++ b/selene-lib/src/lints/deprecated.rs @@ -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"), }; diff --git a/selene-lib/src/lints/standard_library.rs b/selene-lib/src/lints/standard_library.rs index 4ccdc2ac..703f278c 100644 --- a/selene-lib/src/lints/standard_library.rs +++ b/selene-lib/src/lints/standard_library.rs @@ -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, @@ -39,6 +39,17 @@ impl Lint for StandardLibraryLint { } } +fn same_type_if_equal(lhs: &Expression, rhs: &Expression) -> Option { + 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 @@ -57,6 +68,8 @@ fn get_argument_type(expression: &ast::Expression) -> Option 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, } } @@ -155,16 +168,17 @@ fn get_argument_type(expression: &ast::Expression) -> Option 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()), @@ -177,18 +191,6 @@ fn get_argument_type(expression: &ast::Expression) -> Option 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, } }