1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
use std::collections::{BTreeSet, HashMap, HashSet};
use std::env;
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::process::{self, Command, ExitStatus};
use std::str;
use anyhow::{bail, Context, Error};
use cargo_util::{exit_status_to_string, is_simple_exit_code, paths, ProcessBuilder};
use log::{debug, trace, warn};
use rustfix::diagnostics::Diagnostic;
use rustfix::{self, CodeFix};
use semver::Version;
use crate::core::compiler::{CompileKind, RustcTargetData, TargetInfo};
use crate::core::resolver::features::{DiffMap, FeatureOpts, FeatureResolver};
use crate::core::resolver::{HasDevUnits, Resolve, ResolveBehavior};
use crate::core::{Edition, MaybePackage, PackageId, Workspace};
use crate::ops::resolve::WorkspaceResolve;
use crate::ops::{self, CompileOptions};
use crate::util::diagnostic_server::{Message, RustfixDiagnosticServer};
use crate::util::errors::CargoResult;
use crate::util::Config;
use crate::util::{existing_vcs_repo, LockServer, LockServerClient};
use crate::{drop_eprint, drop_eprintln};
const FIX_ENV: &str = "__CARGO_FIX_PLZ";
const BROKEN_CODE_ENV: &str = "__CARGO_FIX_BROKEN_CODE";
const EDITION_ENV: &str = "__CARGO_FIX_EDITION";
const IDIOMS_ENV: &str = "__CARGO_FIX_IDIOMS";
const SUPPORTS_FORCE_WARN: &str = "__CARGO_SUPPORTS_FORCE_WARN";
pub struct FixOptions {
pub edition: bool,
pub idioms: bool,
pub compile_opts: CompileOptions,
pub allow_dirty: bool,
pub allow_no_vcs: bool,
pub allow_staged: bool,
pub broken_code: bool,
}
pub fn fix(ws: &Workspace<'_>, opts: &mut FixOptions) -> CargoResult<()> {
check_version_control(ws.config(), opts)?;
if opts.edition {
check_resolver_change(ws, opts)?;
}
let lock_server = LockServer::new()?;
let mut wrapper = ProcessBuilder::new(env::current_exe()?);
wrapper.env(FIX_ENV, lock_server.addr().to_string());
let _started = lock_server.start()?;
opts.compile_opts.build_config.force_rebuild = true;
if opts.broken_code {
wrapper.env(BROKEN_CODE_ENV, "1");
}
if opts.edition {
wrapper.env(EDITION_ENV, "1");
}
if opts.idioms {
wrapper.env(IDIOMS_ENV, "1");
}
*opts
.compile_opts
.build_config
.rustfix_diagnostic_server
.borrow_mut() = Some(RustfixDiagnosticServer::new()?);
if let Some(server) = opts
.compile_opts
.build_config
.rustfix_diagnostic_server
.borrow()
.as_ref()
{
server.configure(&mut wrapper);
}
let rustc = ws.config().load_global_rustc(Some(ws))?;
wrapper.arg(&rustc.path);
let target_info = TargetInfo::new(
ws.config(),
&opts.compile_opts.build_config.requested_kinds,
&rustc,
CompileKind::Host,
)?;
if target_info.supports_force_warn {
wrapper.env(SUPPORTS_FORCE_WARN, "1");
}
opts.compile_opts.build_config.primary_unit_rustc = Some(wrapper);
ops::compile(ws, &opts.compile_opts)?;
Ok(())
}
fn check_version_control(config: &Config, opts: &FixOptions) -> CargoResult<()> {
if opts.allow_no_vcs {
return Ok(());
}
if !existing_vcs_repo(config.cwd(), config.cwd()) {
bail!(
"no VCS found for this package and `cargo fix` can potentially \
perform destructive changes; if you'd like to suppress this \
error pass `--allow-no-vcs`"
)
}
if opts.allow_dirty && opts.allow_staged {
return Ok(());
}
let mut dirty_files = Vec::new();
let mut staged_files = Vec::new();
if let Ok(repo) = git2::Repository::discover(config.cwd()) {
let mut repo_opts = git2::StatusOptions::new();
repo_opts.include_ignored(false);
for status in repo.statuses(Some(&mut repo_opts))?.iter() {
if let Some(path) = status.path() {
match status.status() {
git2::Status::CURRENT => (),
git2::Status::INDEX_NEW
| git2::Status::INDEX_MODIFIED
| git2::Status::INDEX_DELETED
| git2::Status::INDEX_RENAMED
| git2::Status::INDEX_TYPECHANGE => {
if !opts.allow_staged {
staged_files.push(path.to_string())
}
}
_ => {
if !opts.allow_dirty {
dirty_files.push(path.to_string())
}
}
};
}
}
}
if dirty_files.is_empty() && staged_files.is_empty() {
return Ok(());
}
let mut files_list = String::new();
for file in dirty_files {
files_list.push_str(" * ");
files_list.push_str(&file);
files_list.push_str(" (dirty)\n");
}
for file in staged_files {
files_list.push_str(" * ");
files_list.push_str(&file);
files_list.push_str(" (staged)\n");
}
bail!(
"the working directory of this package has uncommitted changes, and \
`cargo fix` can potentially perform destructive changes; if you'd \
like to suppress this error pass `--allow-dirty`, `--allow-staged`, \
or commit the changes to these files:\n\
\n\
{}\n\
",
files_list
);
}
fn check_resolver_change(ws: &Workspace<'_>, opts: &FixOptions) -> CargoResult<()> {
let root = ws.root_maybe();
match root {
MaybePackage::Package(root_pkg) => {
if root_pkg.manifest().resolve_behavior().is_some() {
return Ok(());
}
let pkgs = opts.compile_opts.spec.get_packages(ws)?;
if !pkgs.iter().any(|&pkg| pkg == root_pkg) {
return Ok(());
}
if root_pkg.manifest().edition() != Edition::Edition2018 {
return Ok(());
}
}
MaybePackage::Virtual(_vm) => {
return Ok(());
}
}
assert_eq!(ws.resolve_behavior(), ResolveBehavior::V1);
let specs = opts.compile_opts.spec.to_package_id_specs(ws)?;
let target_data = RustcTargetData::new(ws, &opts.compile_opts.build_config.requested_kinds)?;
let resolve_differences = |has_dev_units| -> CargoResult<(WorkspaceResolve<'_>, DiffMap)> {
let ws_resolve = ops::resolve_ws_with_opts(
ws,
&target_data,
&opts.compile_opts.build_config.requested_kinds,
&opts.compile_opts.cli_features,
&specs,
has_dev_units,
crate::core::resolver::features::ForceAllTargets::No,
)?;
let feature_opts = FeatureOpts::new_behavior(ResolveBehavior::V2, has_dev_units);
let v2_features = FeatureResolver::resolve(
ws,
&target_data,
&ws_resolve.targeted_resolve,
&ws_resolve.pkg_set,
&opts.compile_opts.cli_features,
&specs,
&opts.compile_opts.build_config.requested_kinds,
feature_opts,
)?;
let diffs = v2_features.compare_legacy(&ws_resolve.resolved_features);
Ok((ws_resolve, diffs))
};
let (_, without_dev_diffs) = resolve_differences(HasDevUnits::No)?;
let (ws_resolve, mut with_dev_diffs) = resolve_differences(HasDevUnits::Yes)?;
if without_dev_diffs.is_empty() && with_dev_diffs.is_empty() {
return Ok(());
}
with_dev_diffs.retain(|k, vals| without_dev_diffs.get(k) != Some(vals));
let config = ws.config();
config.shell().note(
"Switching to Edition 2021 will enable the use of the version 2 feature resolver in Cargo.",
)?;
drop_eprintln!(
config,
"This may cause some dependencies to be built with fewer features enabled than previously."
);
drop_eprintln!(
config,
"More information about the resolver changes may be found \
at https://doc.rust-lang.org/nightly/edition-guide/rust-2021/default-cargo-resolver.html"
);
drop_eprintln!(
config,
"When building the following dependencies, \
the given features will no longer be used:\n"
);
let show_diffs = |differences: DiffMap| {
for ((pkg_id, for_host), removed) in differences {
drop_eprint!(config, " {}", pkg_id);
if for_host {
drop_eprint!(config, " (as host dependency)");
}
drop_eprint!(config, " removed features: ");
let joined: Vec<_> = removed.iter().map(|s| s.as_str()).collect();
drop_eprintln!(config, "{}", joined.join(", "));
}
drop_eprint!(config, "\n");
};
if !without_dev_diffs.is_empty() {
show_diffs(without_dev_diffs);
}
if !with_dev_diffs.is_empty() {
drop_eprintln!(
config,
"The following differences only apply when building with dev-dependencies:\n"
);
show_diffs(with_dev_diffs);
}
report_maybe_diesel(config, &ws_resolve.targeted_resolve)?;
Ok(())
}
fn report_maybe_diesel(config: &Config, resolve: &Resolve) -> CargoResult<()> {
fn is_broken_diesel(pid: PackageId) -> bool {
pid.name() == "diesel" && pid.version() < &Version::new(1, 4, 8)
}
fn is_broken_diesel_migration(pid: PackageId) -> bool {
pid.name() == "diesel_migrations" && pid.version().major <= 1
}
if resolve.iter().any(is_broken_diesel) && resolve.iter().any(is_broken_diesel_migration) {
config.shell().note(
"\
This project appears to use both diesel and diesel_migrations. These packages have
a known issue where the build may fail due to the version 2 resolver preventing
feature unification between those two packages. Please update to at least diesel 1.4.8
to prevent this issue from happening.
",
)?;
}
Ok(())
}
pub fn fix_maybe_exec_rustc(config: &Config) -> CargoResult<bool> {
let lock_addr = match env::var(FIX_ENV) {
Ok(s) => s,
Err(_) => return Ok(false),
};
let args = FixArgs::get()?;
trace!("cargo-fix as rustc got file {:?}", args.file);
let workspace_rustc = std::env::var("RUSTC_WORKSPACE_WRAPPER")
.map(PathBuf::from)
.ok();
let mut rustc = ProcessBuilder::new(&args.rustc).wrapped(workspace_rustc.as_ref());
rustc.env_remove(FIX_ENV);
trace!("start rustfixing {:?}", args.file);
let fixes = rustfix_crate(&lock_addr, &rustc, &args.file, &args, config)?;
if !fixes.files.is_empty() {
let mut cmd = rustc.build_command();
args.apply(&mut cmd);
cmd.arg("--error-format=json");
debug!("calling rustc for final verification: {:?}", cmd);
let output = cmd.output().context("failed to spawn rustc")?;
if output.status.success() {
for (path, file) in fixes.files.iter() {
Message::Fixed {
file: path.clone(),
fixes: file.fixes_applied,
}
.post()?;
}
}
if output.status.success() && output.stderr.is_empty() {
return Ok(true);
}
if !output.status.success() {
if env::var_os(BROKEN_CODE_ENV).is_none() {
for (path, file) in fixes.files.iter() {
debug!("reverting {:?} due to errors", path);
paths::write(path, &file.original_code)?;
}
}
log_failed_fix(&output.stderr, output.status)?;
}
}
let mut cmd = rustc.build_command();
args.apply(&mut cmd);
for arg in args.format_args {
cmd.arg(arg);
}
debug!("calling rustc to display remaining diagnostics: {:?}", cmd);
exit_with(cmd.status().context("failed to spawn rustc")?);
}
#[derive(Default)]
struct FixedCrate {
files: HashMap<String, FixedFile>,
}
struct FixedFile {
errors_applying_fixes: Vec<String>,
fixes_applied: u32,
original_code: String,
}
fn rustfix_crate(
lock_addr: &str,
rustc: &ProcessBuilder,
filename: &Path,
args: &FixArgs,
config: &Config,
) -> Result<FixedCrate, Error> {
if !args.can_run_rustfix(config)? {
return Ok(FixedCrate::default());
}
let _lock = LockServerClient::lock(&lock_addr.parse()?, "global")?;
let mut fixes = FixedCrate::default();
let mut last_fix_counts = HashMap::new();
let iterations = env::var("CARGO_FIX_MAX_RETRIES")
.ok()
.and_then(|n| n.parse().ok())
.unwrap_or(4);
for _ in 0..iterations {
last_fix_counts.clear();
for (path, file) in fixes.files.iter_mut() {
last_fix_counts.insert(path.clone(), file.fixes_applied);
file.errors_applying_fixes.clear();
}
rustfix_and_fix(&mut fixes, rustc, filename, args, config)?;
let mut progress_yet_to_be_made = false;
for (path, file) in fixes.files.iter_mut() {
if file.errors_applying_fixes.is_empty() {
continue;
}
if file.fixes_applied != *last_fix_counts.get(path).unwrap_or(&0) {
progress_yet_to_be_made = true;
}
}
if !progress_yet_to_be_made {
break;
}
}
for (path, file) in fixes.files.iter_mut() {
for error in file.errors_applying_fixes.drain(..) {
Message::ReplaceFailed {
file: path.clone(),
message: error,
}
.post()?;
}
}
Ok(fixes)
}
fn rustfix_and_fix(
fixes: &mut FixedCrate,
rustc: &ProcessBuilder,
filename: &Path,
args: &FixArgs,
config: &Config,
) -> Result<(), Error> {
let only = HashSet::new();
let mut cmd = rustc.build_command();
cmd.arg("--error-format=json");
args.apply(&mut cmd);
debug!(
"calling rustc to collect suggestions and validate previous fixes: {:?}",
cmd
);
let output = cmd.output().with_context(|| {
format!(
"failed to execute `{}`",
rustc.get_program().to_string_lossy()
)
})?;
if !output.status.success() && env::var_os(BROKEN_CODE_ENV).is_none() {
debug!(
"rustfixing `{:?}` failed, rustc exited with {:?}",
filename,
output.status.code()
);
return Ok(());
}
let fix_mode = env::var_os("__CARGO_FIX_YOLO")
.map(|_| rustfix::Filter::Everything)
.unwrap_or(rustfix::Filter::MachineApplicableOnly);
let stderr = str::from_utf8(&output.stderr).context("failed to parse rustc stderr as UTF-8")?;
let suggestions = stderr
.lines()
.filter(|x| !x.is_empty())
.inspect(|y| trace!("line: {}", y))
.filter_map(|line| serde_json::from_str::<Diagnostic>(line).ok())
.filter_map(|diag| rustfix::collect_suggestions(&diag, &only, fix_mode));
let mut file_map = HashMap::new();
let mut num_suggestion = 0;
let home_path = config.home().as_path_unlocked();
for suggestion in suggestions {
trace!("suggestion");
let file_names = suggestion
.solutions
.iter()
.flat_map(|s| s.replacements.iter())
.map(|r| &r.snippet.file_name);
let file_name = if let Some(file_name) = file_names.clone().next() {
file_name.clone()
} else {
trace!("rejecting as it has no solutions {:?}", suggestion);
continue;
};
if Path::new(&file_name).starts_with(home_path) {
continue;
}
if !file_names.clone().all(|f| f == &file_name) {
trace!("rejecting as it changes multiple files: {:?}", suggestion);
continue;
}
trace!("adding suggestion for {:?}: {:?}", file_name, suggestion);
file_map
.entry(file_name)
.or_insert_with(Vec::new)
.push(suggestion);
num_suggestion += 1;
}
debug!(
"collected {} suggestions for `{}`",
num_suggestion,
filename.display(),
);
for (file, suggestions) in file_map {
let code = match paths::read(file.as_ref()) {
Ok(s) => s,
Err(e) => {
warn!("failed to read `{}`: {}", file, e);
continue;
}
};
let num_suggestions = suggestions.len();
debug!("applying {} fixes to {}", num_suggestions, file);
let fixed_file = fixes
.files
.entry(file.clone())
.or_insert_with(|| FixedFile {
errors_applying_fixes: Vec::new(),
fixes_applied: 0,
original_code: code.clone(),
});
let mut fixed = CodeFix::new(&code);
for suggestion in suggestions.iter().rev() {
match fixed.apply(suggestion) {
Ok(()) => fixed_file.fixes_applied += 1,
Err(e) => fixed_file.errors_applying_fixes.push(e.to_string()),
}
}
let new_code = fixed.finish()?;
paths::write(&file, new_code)?;
}
Ok(())
}
fn exit_with(status: ExitStatus) -> ! {
#[cfg(unix)]
{
use std::io::Write;
use std::os::unix::prelude::*;
if let Some(signal) = status.signal() {
drop(writeln!(
std::io::stderr().lock(),
"child failed with signal `{}`",
signal
));
process::exit(2);
}
}
process::exit(status.code().unwrap_or(3));
}
fn log_failed_fix(stderr: &[u8], status: ExitStatus) -> Result<(), Error> {
let stderr = str::from_utf8(stderr).context("failed to parse rustc stderr as utf-8")?;
let diagnostics = stderr
.lines()
.filter(|x| !x.is_empty())
.filter_map(|line| serde_json::from_str::<Diagnostic>(line).ok());
let mut files = BTreeSet::new();
let mut errors = Vec::new();
for diagnostic in diagnostics {
errors.push(diagnostic.rendered.unwrap_or(diagnostic.message));
for span in diagnostic.spans.into_iter() {
files.insert(span.file_name);
}
}
errors.extend(
stderr
.lines()
.filter(|x| !x.starts_with('{'))
.map(|x| x.to_string()),
);
let mut krate = None;
let mut prev_dash_dash_krate_name = false;
for arg in env::args() {
if prev_dash_dash_krate_name {
krate = Some(arg.clone());
}
if arg == "--crate-name" {
prev_dash_dash_krate_name = true;
} else {
prev_dash_dash_krate_name = false;
}
}
let files = files.into_iter().collect();
let abnormal_exit = if status.code().map_or(false, is_simple_exit_code) {
None
} else {
Some(exit_status_to_string(status))
};
Message::FixFailed {
files,
krate,
errors,
abnormal_exit,
}
.post()?;
Ok(())
}
struct FixArgs {
file: PathBuf,
prepare_for_edition: Option<Edition>,
idioms: bool,
enabled_edition: Option<Edition>,
other: Vec<OsString>,
rustc: PathBuf,
format_args: Vec<String>,
}
impl FixArgs {
fn get() -> Result<FixArgs, Error> {
let rustc = env::args_os()
.nth(1)
.map(PathBuf::from)
.ok_or_else(|| anyhow::anyhow!("expected rustc as first argument"))?;
let mut file = None;
let mut enabled_edition = None;
let mut other = Vec::new();
let mut format_args = Vec::new();
for arg in env::args_os().skip(2) {
let path = PathBuf::from(arg);
if path.extension().and_then(|s| s.to_str()) == Some("rs") && path.exists() {
file = Some(path);
continue;
}
if let Some(s) = path.to_str() {
if let Some(edition) = s.strip_prefix("--edition=") {
enabled_edition = Some(edition.parse()?);
continue;
}
if s.starts_with("--error-format=") || s.starts_with("--json=") {
format_args.push(s.to_string());
continue;
}
}
other.push(path.into());
}
let file = file.ok_or_else(|| anyhow::anyhow!("could not find .rs file in rustc args"))?;
let idioms = env::var(IDIOMS_ENV).is_ok();
let prepare_for_edition = env::var(EDITION_ENV).ok().map(|_| {
enabled_edition
.unwrap_or(Edition::Edition2015)
.saturating_next()
});
Ok(FixArgs {
file,
prepare_for_edition,
idioms,
enabled_edition,
other,
rustc,
format_args,
})
}
fn apply(&self, cmd: &mut Command) {
cmd.arg(&self.file);
cmd.args(&self.other);
if self.prepare_for_edition.is_some() && env::var_os(SUPPORTS_FORCE_WARN).is_some() {
cmd.arg("--cap-lints=allow");
} else {
cmd.arg("--cap-lints=warn");
}
if let Some(edition) = self.enabled_edition {
cmd.arg("--edition").arg(edition.to_string());
if self.idioms && edition.supports_idiom_lint() {
cmd.arg(format!("-Wrust-{}-idioms", edition));
}
}
if let Some(edition) = self.prepare_for_edition {
if edition.supports_compat_lint() {
if env::var_os(SUPPORTS_FORCE_WARN).is_some() {
cmd.arg("--force-warn")
.arg(format!("rust-{}-compatibility", edition));
} else {
cmd.arg("-W").arg(format!("rust-{}-compatibility", edition));
}
}
}
}
fn can_run_rustfix(&self, config: &Config) -> CargoResult<bool> {
let to_edition = match self.prepare_for_edition {
Some(s) => s,
None => {
return Message::Fixing {
file: self.file.display().to_string(),
}
.post()
.and(Ok(true));
}
};
if !to_edition.is_stable() && !config.nightly_features_allowed {
let message = format!(
"`{file}` is on the latest edition, but trying to \
migrate to edition {to_edition}.\n\
Edition {to_edition} is unstable and not allowed in \
this release, consider trying the nightly release channel.",
file = self.file.display(),
to_edition = to_edition
);
return Message::EditionAlreadyEnabled {
message,
edition: to_edition.previous().unwrap(),
}
.post()
.and(Ok(false));
}
let from_edition = self.enabled_edition.unwrap_or(Edition::Edition2015);
if from_edition == to_edition {
let message = format!(
"`{}` is already on the latest edition ({}), \
unable to migrate further",
self.file.display(),
to_edition
);
Message::EditionAlreadyEnabled {
message,
edition: to_edition,
}
.post()
} else {
Message::Migrating {
file: self.file.display().to_string(),
from_edition,
to_edition,
}
.post()
}
.and(Ok(true))
}
}