Add clippy suggestions (#6203)
* add clippy suggestions * revert &/ref change * Update cranelift/isle/isle/src/parser.rs Co-authored-by: Jamey Sharp <jamey@minilop.net> --------- Co-authored-by: Jamey Sharp <jamey@minilop.net>
This commit is contained in:
@@ -186,7 +186,7 @@ impl<'a> Codegen<'a> {
|
||||
}
|
||||
|
||||
fn generate_ctx_trait(&self, code: &mut String) {
|
||||
writeln!(code, "").unwrap();
|
||||
writeln!(code).unwrap();
|
||||
writeln!(
|
||||
code,
|
||||
"/// Context during lowering: an implementation of this trait"
|
||||
@@ -287,7 +287,7 @@ impl<'a> Codegen<'a> {
|
||||
for field in &variant.fields {
|
||||
let name = &self.typeenv.syms[field.name.index()];
|
||||
let ty_name =
|
||||
self.typeenv.types[field.ty.index()].name(&self.typeenv);
|
||||
self.typeenv.types[field.ty.index()].name(self.typeenv);
|
||||
writeln!(code, " {}: {},", name, ty_name).unwrap();
|
||||
}
|
||||
writeln!(code, " }},").unwrap();
|
||||
@@ -301,9 +301,9 @@ impl<'a> Codegen<'a> {
|
||||
}
|
||||
|
||||
fn type_name(&self, typeid: TypeId, by_ref: bool) -> String {
|
||||
match &self.typeenv.types[typeid.index()] {
|
||||
&Type::Primitive(_, sym, _) => self.typeenv.syms[sym.index()].clone(),
|
||||
&Type::Enum { name, .. } => {
|
||||
match self.typeenv.types[typeid.index()] {
|
||||
Type::Primitive(_, sym, _) => self.typeenv.syms[sym.index()].clone(),
|
||||
Type::Enum { name, .. } => {
|
||||
let r = if by_ref { "&" } else { "" };
|
||||
format!("{}{}", r, self.typeenv.syms[name.index()])
|
||||
}
|
||||
@@ -620,7 +620,7 @@ impl<'a> Codegen<'a> {
|
||||
&self.typeenv.syms[field.name.index()],
|
||||
)?;
|
||||
self.emit_expr(ctx, *value)?;
|
||||
if ctx.is_ref.contains(&value) {
|
||||
if ctx.is_ref.contains(value) {
|
||||
write!(ctx.out, ".clone()")?;
|
||||
}
|
||||
writeln!(ctx.out, ",")?;
|
||||
|
||||
@@ -19,13 +19,13 @@ impl std::fmt::Debug for Errors {
|
||||
}
|
||||
let diagnostics = Vec::from_iter(self.errors.iter().map(|e| {
|
||||
let message = match e {
|
||||
Error::IoError { context, .. } => format!("{}", context),
|
||||
Error::IoError { context, .. } => context.clone(),
|
||||
Error::ParseError { msg, .. } => format!("parse error: {}", msg),
|
||||
Error::TypeError { msg, .. } => format!("type error: {}", msg),
|
||||
Error::UnreachableError { msg, .. } => format!("unreachable rule: {}", msg),
|
||||
Error::OverlapError { msg, .. } => format!("overlap error: {}", msg),
|
||||
Error::ShadowedError { .. } => {
|
||||
format!("more general higher-priority rule shadows other rules")
|
||||
"more general higher-priority rule shadows other rules".to_string()
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -110,7 +110,7 @@ impl<'a> Lexer<'a> {
|
||||
let mut buf = String::new();
|
||||
for text in &file_texts {
|
||||
file_starts.push(buf.len());
|
||||
buf += &text;
|
||||
buf += text;
|
||||
buf += "\n";
|
||||
}
|
||||
|
||||
@@ -316,18 +316,12 @@ impl<'a> Lexer<'a> {
|
||||
impl Token {
|
||||
/// Is this an `Int` token?
|
||||
pub fn is_int(&self) -> bool {
|
||||
match self {
|
||||
Token::Int(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
matches!(self, Token::Int(_))
|
||||
}
|
||||
|
||||
/// Is this a `Sym` token?
|
||||
pub fn is_sym(&self) -> bool {
|
||||
match self {
|
||||
Token::Symbol(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
matches!(self, Token::Symbol(_))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
fn is<F: Fn(&Token) -> bool>(&self, f: F) -> bool {
|
||||
if let Some(&(_, ref peek)) = self.lexer.peek() {
|
||||
if let Some((_, peek)) = self.lexer.peek() {
|
||||
f(peek)
|
||||
} else {
|
||||
false
|
||||
@@ -99,7 +99,7 @@ impl<'a> Parser<'a> {
|
||||
|
||||
fn is_const(&self) -> bool {
|
||||
self.is(|tok| match tok {
|
||||
&Token::Symbol(ref tok_s) if tok_s.starts_with("$") => true,
|
||||
Token::Symbol(tok_s) if tok_s.starts_with('$') => true,
|
||||
_ => false,
|
||||
})
|
||||
}
|
||||
@@ -123,7 +123,7 @@ impl<'a> Parser<'a> {
|
||||
|
||||
fn eat_sym_str(&mut self, s: &str) -> Result<bool> {
|
||||
self.eat(|tok| match tok {
|
||||
&Token::Symbol(ref tok_s) if tok_s == s => true,
|
||||
Token::Symbol(ref tok_s) if tok_s == s => true,
|
||||
_ => false,
|
||||
})
|
||||
.map(|token| token.is_some())
|
||||
@@ -202,8 +202,7 @@ impl<'a> Parser<'a> {
|
||||
fn parse_const(&mut self) -> Result<Ident> {
|
||||
let pos = self.pos();
|
||||
let ident = self.parse_ident()?;
|
||||
if ident.0.starts_with("$") {
|
||||
let s = &ident.0[1..];
|
||||
if let Some(s) = ident.0.strip_prefix('$') {
|
||||
Ok(Ident(s.to_string(), ident.1))
|
||||
} else {
|
||||
Err(self.error(
|
||||
@@ -216,9 +215,8 @@ impl<'a> Parser<'a> {
|
||||
fn parse_pragma(&mut self) -> Result<Pragma> {
|
||||
let ident = self.parse_ident()?;
|
||||
// currently, no pragmas are defined, but the infrastructure is useful to keep around
|
||||
match ident.0.as_str() {
|
||||
pragma => Err(self.error(ident.1, format!("Unknown pragma '{}'", pragma))),
|
||||
}
|
||||
let pragma = ident.0.as_str();
|
||||
Err(self.error(ident.1, format!("Unknown pragma '{}'", pragma)))
|
||||
}
|
||||
|
||||
fn parse_type(&mut self) -> Result<Type> {
|
||||
@@ -472,7 +470,7 @@ impl<'a> Parser<'a> {
|
||||
self.expect_rparen()?;
|
||||
Ok(ret)
|
||||
} else {
|
||||
self.parse_expr().map(|expr| IfLetOrExpr::Expr(expr))
|
||||
self.parse_expr().map(IfLetOrExpr::Expr)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -595,14 +595,14 @@ pub trait PatternVisitor {
|
||||
impl Pattern {
|
||||
/// Get this pattern's type.
|
||||
pub fn ty(&self) -> TypeId {
|
||||
match self {
|
||||
&Self::BindPattern(t, ..) => t,
|
||||
&Self::Var(t, ..) => t,
|
||||
&Self::ConstInt(t, ..) => t,
|
||||
&Self::ConstPrim(t, ..) => t,
|
||||
&Self::Term(t, ..) => t,
|
||||
&Self::Wildcard(t, ..) => t,
|
||||
&Self::And(t, ..) => t,
|
||||
match *self {
|
||||
Self::BindPattern(t, ..) => t,
|
||||
Self::Var(t, ..) => t,
|
||||
Self::ConstInt(t, ..) => t,
|
||||
Self::ConstPrim(t, ..) => t,
|
||||
Self::Term(t, ..) => t,
|
||||
Self::Wildcard(t, ..) => t,
|
||||
Self::And(t, ..) => t,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -614,14 +614,14 @@ impl Pattern {
|
||||
termenv: &TermEnv,
|
||||
vars: &mut HashMap<VarId, V::PatternId>,
|
||||
) {
|
||||
match self {
|
||||
&Pattern::BindPattern(_ty, var, ref subpat) => {
|
||||
match *self {
|
||||
Pattern::BindPattern(_ty, var, ref subpat) => {
|
||||
// Bind the appropriate variable and recurse.
|
||||
assert!(!vars.contains_key(&var));
|
||||
vars.insert(var, input);
|
||||
subpat.visit(visitor, input, termenv, vars);
|
||||
}
|
||||
&Pattern::Var(ty, var) => {
|
||||
Pattern::Var(ty, var) => {
|
||||
// Assert that the value matches the existing bound var.
|
||||
let var_val = vars
|
||||
.get(&var)
|
||||
@@ -629,9 +629,9 @@ impl Pattern {
|
||||
.expect("Variable should already be bound");
|
||||
visitor.add_match_equal(input, var_val, ty);
|
||||
}
|
||||
&Pattern::ConstInt(ty, value) => visitor.add_match_int(input, ty, value),
|
||||
&Pattern::ConstPrim(ty, value) => visitor.add_match_prim(input, ty, value),
|
||||
&Pattern::Term(ty, term, ref args) => {
|
||||
Pattern::ConstInt(ty, value) => visitor.add_match_int(input, ty, value),
|
||||
Pattern::ConstPrim(ty, value) => visitor.add_match_prim(input, ty, value),
|
||||
Pattern::Term(ty, term, ref args) => {
|
||||
// Determine whether the term has an external extractor or not.
|
||||
let termdata = &termenv.terms[term.index()];
|
||||
let arg_values = match &termdata.kind {
|
||||
@@ -673,12 +673,12 @@ impl Pattern {
|
||||
pat.visit(visitor, val, termenv, vars);
|
||||
}
|
||||
}
|
||||
&Pattern::And(_ty, ref children) => {
|
||||
Pattern::And(_ty, ref children) => {
|
||||
for child in children {
|
||||
child.visit(visitor, input, termenv, vars);
|
||||
}
|
||||
}
|
||||
&Pattern::Wildcard(_ty) => {
|
||||
Pattern::Wildcard(_ty) => {
|
||||
// Nothing!
|
||||
}
|
||||
}
|
||||
@@ -719,12 +719,12 @@ pub trait ExprVisitor {
|
||||
impl Expr {
|
||||
/// Get this expression's type.
|
||||
pub fn ty(&self) -> TypeId {
|
||||
match self {
|
||||
&Self::Term(t, ..) => t,
|
||||
&Self::Var(t, ..) => t,
|
||||
&Self::ConstInt(t, ..) => t,
|
||||
&Self::ConstPrim(t, ..) => t,
|
||||
&Self::Let { ty: t, .. } => t,
|
||||
match *self {
|
||||
Self::Term(t, ..) => t,
|
||||
Self::Var(t, ..) => t,
|
||||
Self::ConstInt(t, ..) => t,
|
||||
Self::ConstPrim(t, ..) => t,
|
||||
Self::Let { ty: t, .. } => t,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -736,10 +736,10 @@ impl Expr {
|
||||
vars: &HashMap<VarId, V::ExprId>,
|
||||
) -> V::ExprId {
|
||||
log!("Expr::visit: expr {:?}", self);
|
||||
match self {
|
||||
&Expr::ConstInt(ty, val) => visitor.add_const_int(ty, val),
|
||||
&Expr::ConstPrim(ty, val) => visitor.add_const_prim(ty, val),
|
||||
&Expr::Let {
|
||||
match *self {
|
||||
Expr::ConstInt(ty, val) => visitor.add_const_int(ty, val),
|
||||
Expr::ConstPrim(ty, val) => visitor.add_const_prim(ty, val),
|
||||
Expr::Let {
|
||||
ty: _ty,
|
||||
ref bindings,
|
||||
ref body,
|
||||
@@ -751,8 +751,8 @@ impl Expr {
|
||||
}
|
||||
body.visit(visitor, termenv, &vars)
|
||||
}
|
||||
&Expr::Var(_ty, var_id) => *vars.get(&var_id).unwrap(),
|
||||
&Expr::Term(ty, term, ref arg_exprs) => {
|
||||
Expr::Var(_ty, var_id) => *vars.get(&var_id).unwrap(),
|
||||
Expr::Term(ty, term, ref arg_exprs) => {
|
||||
let termdata = &termenv.terms[term.index()];
|
||||
let arg_values_tys = arg_exprs
|
||||
.iter()
|
||||
@@ -957,23 +957,21 @@ impl TypeEnv {
|
||||
|
||||
// Now collect types for extern constants.
|
||||
for def in &defs.defs {
|
||||
match def {
|
||||
&ast::Def::Extern(ast::Extern::Const {
|
||||
ref name,
|
||||
ref ty,
|
||||
pos,
|
||||
}) => {
|
||||
let ty = match tyenv.get_type_by_name(ty) {
|
||||
Some(ty) => ty,
|
||||
None => {
|
||||
tyenv.report_error(pos, "Unknown type for constant");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let name = tyenv.intern_mut(name);
|
||||
tyenv.const_types.insert(name, ty);
|
||||
}
|
||||
_ => {}
|
||||
if let &ast::Def::Extern(ast::Extern::Const {
|
||||
ref name,
|
||||
ref ty,
|
||||
pos,
|
||||
}) = def
|
||||
{
|
||||
let ty = match tyenv.get_type_by_name(ty) {
|
||||
Some(ty) => ty,
|
||||
None => {
|
||||
tyenv.report_error(pos, "Unknown type for constant");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let name = tyenv.intern_mut(name);
|
||||
tyenv.const_types.insert(name, ty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1221,7 +1219,6 @@ impl TermEnv {
|
||||
.map(|id| {
|
||||
tyenv.get_type_by_name(id).ok_or_else(|| {
|
||||
tyenv.report_error(id.1, format!("Unknown arg type: '{}'", id.0));
|
||||
()
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>();
|
||||
|
||||
Reference in New Issue
Block a user