-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Compiler-V2] Check Unused Assignments (#13543)
* add unused assignment checker * update tests * add checker * update tests * format * add test * reformat err msg * refactor * update tests * update tests * update tests * update tests * format * add test * import more tests * tab to space * add test * remove wrong doc * add test * update tests --------- Co-authored-by: Zekun Wang <zekun.wang@aptoslabs.com>
- Loading branch information
Showing
108 changed files
with
2,465 additions
and
31 deletions.
There are no files selected for viewing
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
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
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
84 changes: 84 additions & 0 deletions
84
third_party/move/move-compiler-v2/src/pipeline/unused_assignment_checker.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,84 @@ | ||
// Copyright © Aptos Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! Implements a pipeline that checks and gives warning on unused assignments. | ||
//! Prerequisite: live variable annotation. | ||
|
||
use crate::pipeline::livevar_analysis_processor::LiveVarAnnotation; | ||
use codespan_reporting::diagnostic::Severity; | ||
use move_binary_format::file_format::CodeOffset; | ||
use move_model::{ast::TempIndex, model::FunctionEnv}; | ||
use move_stackless_bytecode::{ | ||
function_target::{FunctionData, FunctionTarget}, | ||
function_target_pipeline::{FunctionTargetProcessor, FunctionTargetsHolder}, | ||
stackless_bytecode::{AttrId, Bytecode}, | ||
}; | ||
|
||
pub struct UnusedAssignmentChecker {} | ||
|
||
impl UnusedAssignmentChecker { | ||
/// Check if the assignment to `dst` at offset after the position given by `offset` and `after`. | ||
fn check_unused_assignment( | ||
target: &FunctionTarget, | ||
id: AttrId, | ||
offset: CodeOffset, | ||
dst: TempIndex, | ||
) { | ||
let data = target.data; | ||
// only check for user defined variables | ||
if let Some(dst_name) = data.local_names.get(&dst) { | ||
let live_var_info = target | ||
.get_annotations() | ||
.get::<LiveVarAnnotation>() | ||
.expect("live variable annotation") | ||
.get_info_at(offset); | ||
let live_after = &live_var_info.after; | ||
let dst_name = dst_name.display(target.func_env.symbol_pool()).to_string(); | ||
if !dst_name.starts_with('_') && live_after.get(&dst).is_none() { | ||
let loc = target.get_bytecode_loc(id); | ||
target | ||
.global_env() | ||
.diag( | ||
Severity::Warning, | ||
&loc, | ||
&format!("Unused assignment to `{}`. Consider removing or prefixing with an underscore: `_{}`", dst_name, dst_name) | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl FunctionTargetProcessor for UnusedAssignmentChecker { | ||
fn process( | ||
&self, | ||
_targets: &mut FunctionTargetsHolder, | ||
func_env: &FunctionEnv, | ||
data: FunctionData, | ||
_scc_opt: Option<&[FunctionEnv]>, | ||
) -> FunctionData { | ||
if func_env.is_native() { | ||
return data; | ||
} | ||
let target = FunctionTarget::new(func_env, &data); | ||
for (offset, bytecode) in data.code.iter().enumerate() { | ||
let offset = offset as u16; | ||
use Bytecode::*; | ||
match bytecode { | ||
Load(id, dst, _) | Assign(id, dst, _, _) => { | ||
UnusedAssignmentChecker::check_unused_assignment(&target, *id, offset, *dst) | ||
}, | ||
Call(id, dsts, _, _, _) => { | ||
for dst in dsts { | ||
UnusedAssignmentChecker::check_unused_assignment(&target, *id, offset, *dst) | ||
} | ||
}, | ||
_ => {}, | ||
} | ||
} | ||
data | ||
} | ||
|
||
fn name(&self) -> String { | ||
"UnusedAssignmentChecker".to_string() | ||
} | ||
} |
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
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
Oops, something went wrong.