Skip to content

Commit

Permalink
Merge pull request #500 from shiika-lang/clippy
Browse files Browse the repository at this point in the history
Clippy fixes
  • Loading branch information
yhara authored Jul 24, 2023
2 parents 036d4ad + 64e0710 commit 70ce4ee
Show file tree
Hide file tree
Showing 43 changed files with 137 additions and 128 deletions.
9 changes: 4 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,9 @@ end

task :debugify => DEBUG_OUT

task :a => [:fmt, A_OUT]
task :a => [:fmt, A_OUT] do
#task :a => [:fmt] do
# sh "cargo run -- run a.sk"
#sh "cargo run -- run ~/proj/BidirectionalTypechecking/bidi.sk"
#sh "cargo run -- run /Users/yhara/proj/BidirectionalTypechecking/sexp.sk"
#end
# sh "cargo clippy"
# sh "cargo run -- run a.sk"
end

16 changes: 11 additions & 5 deletions lib/shiika_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ pub struct AstMethodSignature {
}

/// A type parameter
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct AstTyParam {
pub name: String,
pub variance: AstVariance,
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum AstVariance {
Invariant,
Covariant, // eg. `in T`
Expand All @@ -131,15 +131,15 @@ pub struct Param {
pub default_expr: Option<AstExpression>,
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct BlockParam {
pub name: String,
pub opt_typ: Option<UnresolvedTypeName>,
}

/// A type name not yet resolved.
/// eg. for `A::B<C>`, `names` is `A, B` and `args` is `C`.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct UnresolvedTypeName {
pub names: Vec<String>,
pub args: Vec<UnresolvedTypeName>,
Expand Down Expand Up @@ -265,6 +265,12 @@ pub struct AstCallArgs {
pub block: Option<Box<AstExpression>>,
}

impl Default for AstCallArgs {
fn default() -> Self {
Self::new()
}
}

impl AstCallArgs {
pub fn new() -> AstCallArgs {
AstCallArgs {
Expand Down Expand Up @@ -301,7 +307,7 @@ impl AstCallArgs {
.chain(self.named.iter().map(|(_, e)| e))
.collect::<Vec<_>>();
if let Some(e) = &self.block {
v.push(&e);
v.push(e);
}
v
}
Expand Down
6 changes: 3 additions & 3 deletions lib/shiika_ast/src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt;
use std::path::PathBuf;
use std::rc::Rc;

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Location {
pub line: usize,
pub col: usize,
Expand All @@ -16,7 +16,7 @@ impl Location {
}

/// Range in a source file (end-exclusive)
#[derive(PartialEq, Clone)]
#[derive(PartialEq, Eq, Clone)]
pub enum LocationSpan {
Empty,
Just {
Expand Down Expand Up @@ -77,7 +77,7 @@ impl LocationSpan {
end,
..
},
) if filepath == filepath2 => Self::new(&filepath, begin.clone(), end.clone()),
) if filepath == filepath2 => Self::new(filepath, begin.clone(), end.clone()),
_ => {
println!(
"[BUG] invalid LocationSpan (begin: {:?}, end: {:?})",
Expand Down
2 changes: 1 addition & 1 deletion lib/shiika_core/src/names/class_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::type_name::*;
use crate::ty::TermTy;
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub struct ClassFirstname(pub String);

impl std::fmt::Display for ClassFirstname {
Expand Down
4 changes: 2 additions & 2 deletions lib/shiika_core/src/names/const_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ pub fn toplevel_const(first_name: &str) -> ConstFullname {
}

/// A const name not resolved yet
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct UnresolvedConstName(pub Vec<String>);

/// Fully qualified const name.
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub struct ResolvedConstName {
pub names: Vec<String>,
}
Expand Down
15 changes: 11 additions & 4 deletions lib/shiika_core/src/names/method_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl MethodFirstname {
}
}

#[derive(PartialEq, Clone, Eq)]
#[derive(Clone)]
pub struct MethodFullname {
// class/module part
pub type_name: TypeFullname,
Expand All @@ -38,6 +38,13 @@ impl Hash for MethodFullname {
}
}

impl PartialEq for MethodFullname {
fn eq(&self, other: &Self) -> bool {
self.full_name == other.full_name
}
}
impl Eq for MethodFullname {}

impl fmt::Debug for MethodFullname {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "MethodFullname(`{}`)", &self.full_name)
Expand Down Expand Up @@ -67,8 +74,8 @@ impl std::fmt::Display for MethodFullname {
}

impl MethodFullname {
pub fn from_str(v: &str) -> Option<MethodFullname> {
let parts = v.split("#").collect::<Vec<_>>();
pub fn parse(v: &str) -> Option<MethodFullname> {
let parts = v.split('#').collect::<Vec<_>>();
if parts.len() == 2 {
Some(method_fullname_raw(parts[0], parts[1]))
} else {
Expand Down Expand Up @@ -106,7 +113,7 @@ impl<'de> de::Visitor<'de> for MethodFullnameVisitor {
where
E: serde::de::Error,
{
match MethodFullname::from_str(v) {
match MethodFullname::parse(v) {
Some(n) => Ok(n),
None => Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Str(v),
Expand Down
2 changes: 1 addition & 1 deletion lib/shiika_core/src/names/module_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::const_name::{const_fullname, ConstFullname};
use super::type_name::{type_fullname, TypeFullname};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub struct ModuleFirstname(pub String);

impl std::fmt::Display for ModuleFirstname {
Expand Down
2 changes: 1 addition & 1 deletion lib/shiika_core/src/names/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::const_name::*;
use super::module_name::*;
use super::type_name::{type_fullname, TypeFullname};

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Namespace(pub Vec<String>);

impl std::fmt::Display for Namespace {
Expand Down
2 changes: 1 addition & 1 deletion lib/shiika_core/src/ty/erasure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::names::{
use crate::ty::{self, TermTy};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct Erasure {
pub base_name: String,
/// `true` if values of this type are classes
Expand Down
2 changes: 1 addition & 1 deletion lib/shiika_core/src/ty/lit_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,6 @@ impl LitTy {
nom::combinator::opt(nom::sequence::delimited(tag("<"), parse_tys, tag(">")))(s)?;
let type_args = tyargs.unwrap_or_default();

Ok((s, LitTy::new(base_name.to_string(), type_args, is_meta)))
Ok((s, LitTy::new(base_name, type_args, is_meta)))
}
}
4 changes: 2 additions & 2 deletions lib/shiika_core/src/ty/typaram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use nom::IResult;
use serde::{Deserialize, Serialize};

/// A type parameter
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct TyParam {
pub name: String,
pub variance: Variance,
pub upper_bound: LitTy,
pub lower_bound: LitTy,
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub enum Variance {
Invariant,
Covariant, // eg. `in T`
Expand Down
8 changes: 3 additions & 5 deletions lib/shiika_parser/src/definition_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,7 @@ impl<'a> Parser<'a> {

// `foo(bar) -> Baz`
let (sig, is_class_method) = self.parse_method_signature()?;
if sig.name.0 != "[]="
&& (sig.name.0.chars().last().unwrap() == '=')
&& sig.params.len() != 1
{
if sig.name.0 != "[]=" && (sig.name.0.ends_with('=')) && sig.params.len() != 1 {
return Err(parse_error!(
self,
"setter accepts one argument but {:?} were given",
Expand Down Expand Up @@ -348,6 +345,7 @@ impl<'a> Parser<'a> {

self.lv -= 1;
let is_initializer = sig.name.0 == "initialize";
#[allow(clippy::collapsible_else_if)]
if is_class_method {
if is_initializer {
let d = shiika_ast::InitializerDefinition { sig, body_exprs };
Expand Down Expand Up @@ -384,7 +382,7 @@ impl<'a> Parser<'a> {
self.ast.bare_name(&param.name, loc.clone(), loc.clone()),
readonly,
loc.clone(),
loc.clone(),
loc,
)
})
.collect()
Expand Down
4 changes: 2 additions & 2 deletions lib/shiika_parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct Lexer<'a> {
/// - `p - x` # binary minus ExprArg
/// - `p -x` # unary minus ExprArg
/// - `1 -2` # binary minus (unusual) ExprArg
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub enum LexerState {
/// A new expression begins here
/// `+`/`-` is always unary
Expand All @@ -50,7 +50,7 @@ pub enum LexerState {
StrLiteral,
}

#[derive(Debug, PartialEq, Clone, Default)]
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct Cursor {
line: usize,
col: usize,
Expand Down
6 changes: 3 additions & 3 deletions lib/skc_ast2hir/src/class_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use type_index::TypeIndex;

type RustMethods = HashMap<TypeFullname, Vec<MethodSignature>>;

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub struct ClassDict<'hir_maker> {
/// List of classes (without method) collected prior to sk_types
type_index: type_index::TypeIndex,
Expand All @@ -37,7 +37,7 @@ pub fn create<'hir_maker>(
imported_classes,
rust_methods: Default::default(),
};
dict.index_program(&defs)?;
dict.index_program(defs)?;
Ok(dict)
}

Expand All @@ -54,7 +54,7 @@ pub fn create_for_corelib<'hir_maker>(
imported_classes,
rust_methods: index_rust_method_sigs(rust_method_sigs),
};
dict.index_program(&defs)?;
dict.index_program(defs)?;
Ok(dict)
}

Expand Down
4 changes: 2 additions & 2 deletions lib/skc_ast2hir/src/class_dict/indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl<'hir_maker> ClassDict<'hir_maker> {
typarams: Vec<ty::TyParam>,
defs: &[shiika_ast::Definition],
) -> Result<()> {
let fullname = namespace.module_fullname(&firstname);
let fullname = namespace.module_fullname(firstname);
let inner_namespace = namespace.add(firstname.to_string());
let (instance_methods, class_methods, requirements) =
self.index_defs_in_module(&inner_namespace, &fullname, &typarams, defs)?;
Expand Down Expand Up @@ -419,7 +419,7 @@ impl<'hir_maker> ClassDict<'hir_maker> {
shiika_ast::Definition::ClassInitializerDefinition(
shiika_ast::InitializerDefinition { sig, .. },
) => {
if sig.params.len() != 0 {
if !sig.params.is_empty() {
return Err(error::program_error(&format!(
"{}.{} should take no parameters",
namespace, &sig.name
Expand Down
6 changes: 3 additions & 3 deletions lib/skc_ast2hir/src/class_dict/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<'hir_maker> ClassDict<'hir_maker> {
};
let sk_type = self.get_type(&erasure.to_type_fullname());
if let Some(found) = self.find_method(&sk_type.base().fullname(), method_name) {
if method_tyargs.len() > 0 && method_tyargs.len() != found.sig.typarams.len() {
if !method_tyargs.is_empty() && method_tyargs.len() != found.sig.typarams.len() {
return Err(error::type_error(format!(
"wrong number of type arguments, expected: {:?} got: {:?}",
&found.sig.typarams.len(),
Expand All @@ -91,13 +91,13 @@ impl<'hir_maker> ClassDict<'hir_maker> {
if let Some(mut found) =
self.find_method(&modinfo.erasure().to_type_fullname(), method_name)
{
let mod_tyargs = sk_class.specialize_module(modinfo, &class_tyargs);
let mod_tyargs = sk_class.specialize_module(modinfo, class_tyargs);
found.specialize(&mod_tyargs, method_tyargs);
return Ok(found);
}
}
// Look up in superclass
if let Some(super_ty) = &sk_class.specialized_superclass(&class_tyargs) {
if let Some(super_ty) = &sk_class.specialized_superclass(class_tyargs) {
return self.lookup_method_(
receiver_type,
&super_ty.to_term_ty(),
Expand Down
Loading

0 comments on commit 70ce4ee

Please sign in to comment.