-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[move][move-linter] implement public mut context rules
- Loading branch information
Showing
8 changed files
with
127 additions
and
142 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
external-crates/move/crates/move-compiler/src/linters/public_mut_tx_context.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) The Move Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! Enforces that public functions use `&mut TxContext` instead of `&TxContext` to ensure upgradability. | ||
//! Detects and reports instances where a non-mutable reference to `TxContext` is used in public function signatures. | ||
//! Promotes best practices for future-proofing smart contract code by allowing mutation of the transaction context. | ||
use crate::{ | ||
diag, | ||
diagnostics::{ | ||
codes::{custom, DiagnosticInfo, Severity}, | ||
WarningFilters, | ||
}, | ||
expansion::ast::{ModuleIdent, Visibility}, | ||
naming::ast::{TypeName_, Type_}, | ||
parser::ast::{DatatypeName, FunctionName}, | ||
shared::CompilationEnv, | ||
typing::{ | ||
ast as T, | ||
visitor::{TypingVisitorConstructor, TypingVisitorContext}, | ||
}, | ||
}; | ||
use move_ir_types::location::Loc; | ||
|
||
use super::{LinterDiagnosticCategory, LINT_WARNING_PREFIX, REQUIRE_MUTABLE_TX_CONTEXT_DIAG_CODE}; | ||
|
||
const REQUIRE_MUTABLE_TX_CONTEXT_DIAG: DiagnosticInfo = custom( | ||
LINT_WARNING_PREFIX, | ||
Severity::Warning, | ||
LinterDiagnosticCategory::Suspicious as u8, | ||
REQUIRE_MUTABLE_TX_CONTEXT_DIAG_CODE, | ||
"", | ||
); | ||
|
||
pub struct RequireMutableTxContext; | ||
|
||
pub struct Context<'a> { | ||
env: &'a mut CompilationEnv, | ||
} | ||
|
||
impl TypingVisitorConstructor for RequireMutableTxContext { | ||
type Context<'a> = Context<'a>; | ||
|
||
fn context<'a>(env: &'a mut CompilationEnv, _program: &T::Program) -> Self::Context<'a> { | ||
Context { env } | ||
} | ||
} | ||
|
||
impl TypingVisitorContext for Context<'_> { | ||
fn visit_function_custom( | ||
&mut self, | ||
_module: ModuleIdent, | ||
_function_name: FunctionName, | ||
fdef: &mut T::Function, | ||
) -> bool { | ||
if let Visibility::Public(_) = fdef.visibility { | ||
for param in &fdef.signature.parameters { | ||
if let Type_::Ref(false, var_type) = ¶m.2.value { | ||
if let Type_::Apply(_, type_name, _) = &var_type.value { | ||
if let TypeName_::ModuleType(_, DatatypeName(sp!(_, struct_name))) = | ||
&type_name.value | ||
{ | ||
if struct_name.to_string() == "TxContext" { | ||
report_non_mutable_tx_context(self.env, type_name.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 report_non_mutable_tx_context(env: &mut CompilationEnv, loc: Loc) { | ||
let diag = diag!( | ||
REQUIRE_MUTABLE_TX_CONTEXT_DIAG, | ||
(loc, "Public functions should take `&mut TxContext` instead of `&TxContext` for better upgradability.") | ||
); | ||
env.add_diag(diag); | ||
} |
109 changes: 0 additions & 109 deletions
109
external-crates/move/crates/move-compiler/src/linters/shift_overflow.rs
This file was deleted.
Oops, something went wrong.
24 changes: 0 additions & 24 deletions
24
external-crates/move/crates/move-compiler/tests/custom_rules/shift_overflow.exp
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
external-crates/move/crates/move-compiler/tests/custom_rules/shift_overflow.move
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
external-crates/move/crates/move-compiler/tests/linter/correct_public_mut_tx_context.move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module 0x42::M { | ||
use sui::tx_context::TxContext; | ||
public fun mint(_ctx: &mut TxContext) { | ||
|
||
} | ||
} | ||
|
||
module sui::tx_context { | ||
struct TxContext has drop {} | ||
} |
8 changes: 8 additions & 0 deletions
8
external-crates/move/crates/move-compiler/tests/linter/incorrect_public_mut_tx_context.exp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
warning[Lint W02014]: | ||
┌─ tests/linter/incorrect_public_mut_tx_context.move:3:28 | ||
│ | ||
3 │ public fun mint(_ctx: &TxContext) { | ||
│ ^^^^^^^^^ Public functions should take `&mut TxContext` instead of `&TxContext` for better upgradability. | ||
│ | ||
= This warning can be suppressed with '#[allow(lint(public_mut_tx_context))]' applied to the 'module' or module member ('const', 'fun', or 'struct') | ||
|
10 changes: 10 additions & 0 deletions
10
external-crates/move/crates/move-compiler/tests/linter/incorrect_public_mut_tx_context.move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module 0x42::M { | ||
use sui::tx_context::TxContext; | ||
public fun mint(_ctx: &TxContext) { | ||
|
||
} | ||
} | ||
|
||
module sui::tx_context { | ||
struct TxContext has drop {} | ||
} |
11 changes: 11 additions & 0 deletions
11
external-crates/move/crates/move-compiler/tests/linter/suppressed_lints.move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
module 0x42::M { | ||
use sui::tx_context::TxContext; | ||
|
||
#[allow(lint(constant_naming))] | ||
const Another_BadName: u64 = 42; // Should trigger a warning | ||
|
||
#[allow(lint(public_mut_tx_context))] | ||
public fun mint(_ctx: &TxContext) { | ||
|
||
} | ||
} | ||
|
||
|
||
module sui::tx_context { | ||
struct TxContext has drop {} | ||
} |