Skip to content

Commit d71d4c9

Browse files
committed
Fix Clippy and fmt issues
1 parent 2782874 commit d71d4c9

File tree

11 files changed

+307
-165
lines changed

11 files changed

+307
-165
lines changed

forc-plugins/forc-migrate/src/cli/commands/run.rs

Lines changed: 75 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,13 @@ pub(crate) fn exec(command: Command) -> Result<()> {
133133
MigrationStepKind::Instruction(instruction) => {
134134
let occurrences_spans = instruction(&program_info)?;
135135

136-
print_instruction_result(&engines, max_len, feature, migration_step, &occurrences_spans);
136+
print_instruction_result(
137+
&engines,
138+
max_len,
139+
feature,
140+
migration_step,
141+
&occurrences_spans,
142+
);
137143

138144
if !occurrences_spans.is_empty() {
139145
println_yellow_bold("If you've already reviewed the above points, you can ignore this info.");
@@ -142,25 +148,58 @@ pub(crate) fn exec(command: Command) -> Result<()> {
142148
MigrationStepKind::CodeModification(modification, manual_migration_actions) => {
143149
let occurrences_spans = modification(&mut program_info.as_mut(), DryRun::No)?;
144150

145-
output_modified_modules(&build_instructions.manifest_dir()?, &program_info, &occurrences_spans)?;
146-
147-
let stop_migration_process = print_modification_result(max_len, feature, migration_step, manual_migration_actions, &occurrences_spans, &mut current_feature_migration_has_code_changes);
151+
output_modified_modules(
152+
&build_instructions.manifest_dir()?,
153+
&program_info,
154+
&occurrences_spans,
155+
)?;
156+
157+
let stop_migration_process = print_modification_result(
158+
max_len,
159+
feature,
160+
migration_step,
161+
manual_migration_actions,
162+
&occurrences_spans,
163+
&mut current_feature_migration_has_code_changes,
164+
);
148165
if stop_migration_process == StopMigrationProcess::Yes {
149166
return Ok(());
150167
}
151168
}
152-
MigrationStepKind::Interaction(instruction, interaction, manual_migration_actions) => {
169+
MigrationStepKind::Interaction(
170+
instruction,
171+
interaction,
172+
manual_migration_actions,
173+
) => {
153174
let instruction_occurrences_spans = instruction(&program_info)?;
154175

155-
print_instruction_result(&engines, max_len, feature, migration_step, &instruction_occurrences_spans);
176+
print_instruction_result(
177+
&engines,
178+
max_len,
179+
feature,
180+
migration_step,
181+
&instruction_occurrences_spans,
182+
);
156183

157184
// We have occurrences, let's continue with the interaction.
158185
if !instruction_occurrences_spans.is_empty() {
159-
let interaction_occurrences_spans = interaction(&mut program_info.as_mut())?;
160-
161-
output_modified_modules(&build_instructions.manifest_dir()?, &program_info, &interaction_occurrences_spans)?;
162-
163-
let stop_migration_process = print_modification_result(max_len, feature, migration_step, manual_migration_actions, &interaction_occurrences_spans, &mut current_feature_migration_has_code_changes);
186+
let interaction_occurrences_spans =
187+
interaction(&mut program_info.as_mut())?;
188+
189+
output_modified_modules(
190+
&build_instructions.manifest_dir()?,
191+
&program_info,
192+
&interaction_occurrences_spans,
193+
)?;
194+
195+
let stop_migration_process = print_modification_result(
196+
max_len,
197+
feature,
198+
migration_step,
199+
manual_migration_actions,
200+
&interaction_occurrences_spans,
201+
&mut current_feature_migration_has_code_changes,
202+
);
164203
if stop_migration_process == StopMigrationProcess::Yes {
165204
return Ok(());
166205
}
@@ -196,7 +235,14 @@ enum StopMigrationProcess {
196235
No,
197236
}
198237

199-
fn print_modification_result(max_len: usize, feature: &Feature, migration_step: &MigrationStep, manual_migration_actions: &[&str], occurrences_spans: &[Span], current_feature_migration_has_code_changes: &mut bool) -> StopMigrationProcess {
238+
fn print_modification_result(
239+
max_len: usize,
240+
feature: &Feature,
241+
migration_step: &MigrationStep,
242+
manual_migration_actions: &[&str],
243+
occurrences_spans: &[Span],
244+
current_feature_migration_has_code_changes: &mut bool,
245+
) -> StopMigrationProcess {
200246
if occurrences_spans.is_empty() {
201247
print_checked_action(max_len, feature, migration_step);
202248
StopMigrationProcess::No
@@ -233,18 +279,21 @@ fn print_modification_result(max_len: usize, feature: &Feature, migration_step:
233279
}
234280
}
235281

236-
fn print_instruction_result(engines: &Engines, max_len: usize, feature: &Feature, migration_step: &MigrationStep, occurrences_spans: &[Span]) {
282+
fn print_instruction_result(
283+
engines: &Engines,
284+
max_len: usize,
285+
feature: &Feature,
286+
migration_step: &MigrationStep,
287+
occurrences_spans: &[Span],
288+
) {
237289
if occurrences_spans.is_empty() {
238290
print_checked_action(max_len, feature, migration_step);
239291
} else {
240292
print_review_action(max_len, feature, migration_step);
241293

242-
if let Some(diagnostic) = create_migration_diagnostic(
243-
engines.se(),
244-
feature,
245-
migration_step,
246-
&occurrences_spans,
247-
) {
294+
if let Some(diagnostic) =
295+
create_migration_diagnostic(engines.se(), feature, migration_step, occurrences_spans)
296+
{
248297
format_diagnostic(&diagnostic);
249298
}
250299
}
@@ -254,21 +303,20 @@ fn print_instruction_result(engines: &Engines, max_len: usize, feature: &Feature
254303
///
255304
/// A module is considered modified, if any of the [Span]s in `occurrences_spans`
256305
/// has that module as its source.
257-
fn output_modified_modules(manifest_dir: &Path, program_info: &ProgramInfo, occurrences_spans: &[Span]) -> Result<()> {
306+
fn output_modified_modules(
307+
manifest_dir: &Path,
308+
program_info: &ProgramInfo,
309+
occurrences_spans: &[Span],
310+
) -> Result<()> {
258311
if occurrences_spans.is_empty() {
259312
return Ok(());
260313
}
261314

262-
let modified_modules =
263-
ModifiedModules::new(program_info.engines.se(), occurrences_spans);
315+
let modified_modules = ModifiedModules::new(program_info.engines.se(), occurrences_spans);
264316

265317
check_that_modified_modules_are_not_dirty(&modified_modules)?;
266318

267-
output_changed_lexed_program(
268-
manifest_dir,
269-
&modified_modules,
270-
&program_info.lexed_program,
271-
)?;
319+
output_changed_lexed_program(manifest_dir, &modified_modules, &program_info.lexed_program)?;
272320

273321
Ok(())
274322
}

forc-plugins/forc-migrate/src/cli/shared.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,20 @@ pub(crate) fn create_migration_diagnostic(
251251
MigrationStepKind::CodeModification(_, []) => vec![],
252252
MigrationStepKind::CodeModification(_, manual_migration_actions) => {
253253
get_manual_migration_actions_help(manual_migration_actions)
254-
},
254+
}
255255
MigrationStepKind::Interaction(_, _, []) => vec![
256-
"This migration step will interactively modify the code, based on your input.".to_string(),
256+
"This migration step will interactively modify the code, based on your input."
257+
.to_string(),
257258
Diagnostic::help_empty_line(),
258259
],
259260
MigrationStepKind::Interaction(_, _, manual_migration_actions) => vec![
260-
"This migration step will interactively modify the code, based on your input.".to_string(),
261+
"This migration step will interactively modify the code, based on your input."
262+
.to_string(),
261263
Diagnostic::help_empty_line(),
262-
].into_iter().chain(get_manual_migration_actions_help(manual_migration_actions)).collect(),
264+
]
265+
.into_iter()
266+
.chain(get_manual_migration_actions_help(manual_migration_actions))
267+
.collect(),
263268
})
264269
.chain(vec![detailed_migration_guide_msg(feature)])
265270
.collect(),

forc-plugins/forc-migrate/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ mod matching;
55
mod modifying;
66

77
use std::fmt::Display;
8-
use std::io::Write;
9-
use std::{io, usize, vec};
8+
use std::io::{self, Write};
109

1110
/// Returns a single error string formed of the `error` and `instructions`.
1211
/// The returned string is formatted to be used as an error message in the [anyhow::bail] macro.
@@ -31,10 +30,13 @@ fn internal_error<E: Display>(error: E) -> String {
3130
/// Prints a menu containing numbered `options` and asks to choose one of them.
3231
/// Returns zero-indexed index of the chosen option.
3332
fn print_single_choice_menu<S: AsRef<str> + Display>(options: &[S]) -> usize {
34-
assert!(options.len() > 1, "There must be at least two options to choose from.");
33+
assert!(
34+
options.len() > 1,
35+
"There must be at least two options to choose from."
36+
);
3537

3638
for (i, option) in options.iter().enumerate() {
37-
println!("{}. {option}", i+1);
39+
println!("{}. {option}", i + 1);
3840
}
3941

4042
let mut choice = usize::MAX;

forc-plugins/forc-migrate/src/matching/lexed_tree.rs

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,61 @@
11
//! This module contains helper functions for matching elements within a lexed program.
22
3+
use super::{any_mut, LexedElementsMatcher, LexedElementsMatcherDeep};
34
use sway_ast::{ItemKind, ItemStorage, StorageEntry, StorageField};
45
use sway_core::language::lexed::{LexedModule, LexedProgram};
5-
use super::{any_mut, LexedElementsMatcher, LexedElementsMatcherDeep};
66

77
impl LexedElementsMatcher<ItemStorage> for LexedProgram {
8-
fn match_elems<'a, F>(&'a mut self, predicate: F) -> impl Iterator<Item=&'a mut ItemStorage>
8+
fn match_elems<'a, F>(&'a mut self, predicate: F) -> impl Iterator<Item = &'a mut ItemStorage>
99
where
1010
F: Fn(&&'a mut ItemStorage) -> bool + Clone + 'a,
11-
ItemStorage: 'a
11+
ItemStorage: 'a,
1212
{
1313
// Storage can be declared only in the root of a contract.
1414
self.root.match_elems(predicate)
1515
}
1616
}
1717

1818
impl LexedElementsMatcher<ItemStorage> for LexedModule {
19-
fn match_elems<'a, F>(&'a mut self, predicate: F) -> impl Iterator<Item=&'a mut ItemStorage>
19+
fn match_elems<'a, F>(&'a mut self, predicate: F) -> impl Iterator<Item = &'a mut ItemStorage>
2020
where
2121
F: Fn(&&'a mut ItemStorage) -> bool + Clone + 'a,
22-
ItemStorage: 'a
22+
ItemStorage: 'a,
2323
{
24-
self
25-
.tree
24+
self.tree
2625
.items
2726
.iter_mut()
2827
.map(|annotated_item| &mut annotated_item.value)
2928
.filter_map(move |decl| match decl {
30-
ItemKind::Storage(ref mut item_storage) => if predicate(&item_storage) {
31-
Some(item_storage)
32-
} else {
33-
None
29+
ItemKind::Storage(ref mut item_storage) => {
30+
if predicate(&item_storage) {
31+
Some(item_storage)
32+
} else {
33+
None
34+
}
3435
}
3536
_ => None,
3637
})
3738
}
3839
}
3940

4041
impl LexedElementsMatcher<StorageField> for ItemStorage {
41-
fn match_elems<'a, F>(&'a mut self, predicate: F) -> impl Iterator<Item=&'a mut StorageField>
42+
fn match_elems<'a, F>(&'a mut self, predicate: F) -> impl Iterator<Item = &'a mut StorageField>
4243
where
4344
F: Fn(&&'a mut StorageField) -> bool + Clone + 'a,
44-
StorageField: 'a
45+
StorageField: 'a,
4546
{
46-
self
47-
.entries
47+
self.entries
4848
.inner
4949
.iter_mut()
5050
.map(|annotated_item| &mut annotated_item.value)
5151
.filter_map(move |storage_entry| match storage_entry.field {
52-
Some(ref mut sf) => if predicate(&sf) {
53-
Some(sf)
54-
} else {
55-
None
56-
},
52+
Some(ref mut sf) => {
53+
if predicate(&sf) {
54+
Some(sf)
55+
} else {
56+
None
57+
}
58+
}
5759
None => None,
5860
})
5961
}
@@ -63,10 +65,13 @@ impl LexedElementsMatcherDeep<StorageField> for ItemStorage {
6365
fn match_elems_deep<'a, F>(&'a mut self, predicate: F) -> Vec<&'a mut StorageField>
6466
where
6567
F: Fn(&&'a mut StorageField) -> bool + Clone + 'a,
66-
StorageField: 'a
68+
StorageField: 'a,
6769
{
68-
fn recursively_collect_storage_fields_in_storage_entry<'a, P>(result: &mut Vec<&'a mut StorageField>, predicate: P, storage_entry: &'a mut StorageEntry)
69-
where
70+
fn recursively_collect_storage_fields_in_storage_entry<'a, P>(
71+
result: &mut Vec<&'a mut StorageField>,
72+
predicate: P,
73+
storage_entry: &'a mut StorageEntry,
74+
) where
7075
P: Fn(&&'a mut StorageField) -> bool + Clone + 'a,
7176
{
7277
if let Some(ref mut sf) = storage_entry.field {
@@ -80,17 +85,28 @@ impl LexedElementsMatcherDeep<StorageField> for ItemStorage {
8085
.inner
8186
.iter_mut()
8287
.map(|annotated_item| &mut annotated_item.value)
83-
.for_each(|storage_entry| recursively_collect_storage_fields_in_storage_entry(result, predicate.clone(), storage_entry.as_mut()));
88+
.for_each(|storage_entry| {
89+
recursively_collect_storage_fields_in_storage_entry(
90+
result,
91+
predicate.clone(),
92+
storage_entry.as_mut(),
93+
)
94+
});
8495
}
8596
}
8697

8798
let mut result = vec![];
88-
self
89-
.entries
99+
self.entries
90100
.inner
91101
.iter_mut()
92102
.map(|annotated_item| &mut annotated_item.value)
93-
.for_each(|storage_entry| recursively_collect_storage_fields_in_storage_entry(&mut result, predicate.clone(), storage_entry));
103+
.for_each(|storage_entry| {
104+
recursively_collect_storage_fields_in_storage_entry(
105+
&mut result,
106+
predicate.clone(),
107+
storage_entry,
108+
)
109+
});
94110

95111
result
96112
}
@@ -107,15 +123,21 @@ pub mod matchers {
107123
}
108124

109125
#[allow(dead_code)]
110-
pub(crate) fn storage_fields<'a, P, F>(parent: &'a mut P, predicate: F) -> impl Iterator<Item=&'a mut StorageField>
126+
pub(crate) fn storage_fields<'a, P, F>(
127+
parent: &'a mut P,
128+
predicate: F,
129+
) -> impl Iterator<Item = &'a mut StorageField>
111130
where
112131
F: Fn(&&'a mut StorageField) -> bool + Clone + 'a,
113132
P: LexedElementsMatcher<StorageField>,
114133
{
115134
parent.match_elems(predicate)
116135
}
117136

118-
pub(crate) fn storage_fields_deep<'a, S, F>(scope: &'a mut S, predicate: F) -> Vec<&'a mut StorageField>
137+
pub(crate) fn storage_fields_deep<'a, S, F>(
138+
scope: &'a mut S,
139+
predicate: F,
140+
) -> Vec<&'a mut StorageField>
119141
where
120142
F: Fn(&&'a mut StorageField) -> bool + Clone + 'a,
121143
S: LexedElementsMatcherDeep<StorageField>,
@@ -137,4 +159,4 @@ pub mod predicates {
137159
storage_field.key_expr.is_none()
138160
}
139161
}
140-
}
162+
}

0 commit comments

Comments
 (0)