Skip to content

Commit

Permalink
impl rules
Browse files Browse the repository at this point in the history
  • Loading branch information
jamedzung committed Mar 26, 2024
1 parent 008aaa8 commit 1812a85
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 150 deletions.
16 changes: 8 additions & 8 deletions external-crates/move/crates/move-compiler/src/linters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use move_symbol_pool::Symbol;

use crate::{
command_line::compiler::Visitor, diagnostics::codes::WarningFilter,
linters::shift_overflow::ShiftOperationOverflow, typing::visitor::TypingVisitor,
linters::unnecessary_while_loop::WhileTrueToLoop, typing::visitor::TypingVisitor,
};
pub mod shift_overflow;
pub mod unnecessary_while_loop;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LintLevel {
// No linters
Expand All @@ -20,22 +20,22 @@ pub enum LintLevel {

pub const ALLOW_ATTR_CATEGORY: &str = "lint";
pub const LINT_WARNING_PREFIX: &str = "Lint ";
pub const SHILF_OVERFLOW_FILTER_NAME: &str = "shift_overflow";
pub const WHILE_TRUE_TO_LOOP_FILTER_NAME: &str = "unnecessary_while_loop";

pub const LINTER_DEFAULT_DIAG_CODE: u8 = 1;

pub enum LinterDiagCategory {
ShiftOperationOverflow,
WhileTrueToLoop,
}

pub fn known_filters() -> (Option<Symbol>, Vec<WarningFilter>) {
(
Some(ALLOW_ATTR_CATEGORY.into()),
vec![WarningFilter::code(
Some(LINT_WARNING_PREFIX),
LinterDiagCategory::ShiftOperationOverflow as u8,
LinterDiagCategory::WhileTrueToLoop as u8,
LINTER_DEFAULT_DIAG_CODE,
Some(SHILF_OVERFLOW_FILTER_NAME),
Some(WHILE_TRUE_TO_LOOP_FILTER_NAME),
)],
)
}
Expand All @@ -44,8 +44,8 @@ pub fn linter_visitors(level: LintLevel) -> Vec<Visitor> {
match level {
LintLevel::None => vec![],
LintLevel::Default | LintLevel::All => {
vec![shift_overflow::ShiftOperationOverflow::visitor(
ShiftOperationOverflow,
vec![unnecessary_while_loop::WhileTrueToLoop::visitor(
WhileTrueToLoop,
)]
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//! Encourages replacing `while(true)` with `loop` for infinite loops in Rust for clarity and conciseness.
//! Identifies `while(true)` patterns, suggesting a more idiomatic approach using `loop`.
//! Aims to enhance code readability and adherence to Rust idioms.
use crate::{
diag,
diagnostics::{
codes::{custom, DiagnosticInfo, Severity},
WarningFilters,
},
expansion::ast::Value_,
shared::{program_info::TypingProgramInfo, CompilationEnv},
typing::{
ast::{self as T, UnannotatedExp_},
visitor::{TypingVisitorConstructor, TypingVisitorContext},
},
};
use move_ir_types::location::Loc;

use super::{LinterDiagCategory, LINTER_DEFAULT_DIAG_CODE, LINT_WARNING_PREFIX};

const WHILE_TRUE_TO_LOOP_DIAG: DiagnosticInfo = custom(
LINT_WARNING_PREFIX,
Severity::Warning,
LinterDiagCategory::WhileTrueToLoop as u8,
LINTER_DEFAULT_DIAG_CODE,
"",
);

pub struct WhileTrueToLoop;

pub struct Context<'a> {
env: &'a mut CompilationEnv,
}

impl TypingVisitorConstructor for WhileTrueToLoop {
type Context<'a> = Context<'a>;

fn context<'a>(
env: &'a mut CompilationEnv,
_program_info: &'a TypingProgramInfo,
_program: &T::Program_,
) -> Self::Context<'a> {
Context { env }
}
}

impl TypingVisitorContext for Context<'_> {
fn visit_exp_custom(&mut self, exp: &mut T::Exp) -> bool {
if let UnannotatedExp_::While(_, cond, _) = &exp.exp.value {
if is_condition_always_true(&cond.exp.value) {
report_while_true_to_loop(self.env, exp.exp.loc);
}
}
false
}
fn add_warning_filter_scope(&mut self, filter: WarningFilters) {
self.env.add_warning_filter_scope(filter)
}

fn pop_warning_filter_scope(&mut self) {
self.env.pop_warning_filter_scope()
}
}

fn is_condition_always_true(condition: &UnannotatedExp_) -> bool {
if let UnannotatedExp_::Value(val) = condition {
if let Value_::Bool(b) = &val.value {
return *b;
}
}
false
}
fn report_while_true_to_loop(env: &mut CompilationEnv, loc: Loc) {
let diag = diag!(
WHILE_TRUE_TO_LOOP_DIAG,
(
loc,
"Detected `while(true) {}` loop. Consider replacing with `loop {}`"
)
);
env.add_diag(diag);
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
warning[Lint W00001]:
┌─ tests/custom_rules/unnecessary_while_loop.move:5:9
5 │ ╭ while (true) {
6 │ │ if(counter == 10) {
7 │ │ break
8 │ │ };
9 │ │ counter = counter + 1;
10 │ │ }
│ ╰─────────^ Detected `while(true) {}` loop. Consider replacing with `loop {}`
= This warning can be suppressed with '#[allow(lint(unnecessary_while_loop))]' applied to the 'module' or module member ('const', 'fun', or 'struct')

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module 0x42::M {

public fun finite_loop() {
let counter = 0;
while (true) {
if(counter == 10) {
break
};
counter = counter + 1;
}
}
}

0 comments on commit 1812a85

Please sign in to comment.