Skip to content

Commit

Permalink
Move suggest_ref_mut into rustc_mir::borrow_check
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjasper committed Jul 14, 2019
1 parent 2cc2b94 commit abfd4d1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
15 changes: 14 additions & 1 deletion src/librustc_mir/borrow_check/mutability_errors.rs
@@ -1,3 +1,4 @@
use core::unicode::property::Pattern_White_Space;
use rustc::hir;
use rustc::hir::Node;
use rustc::mir::{self, BindingForm, ClearCrossCrate, Local, Location, Body};
Expand All @@ -10,7 +11,6 @@ use syntax_pos::symbol::kw;
use crate::borrow_check::MirBorrowckCtxt;
use crate::borrow_check::error_reporting::BorrowedContentSource;
use crate::util::collect_writes::FindAssignments;
use crate::util::suggest_ref_mut;
use rustc_errors::Applicability;

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -628,3 +628,16 @@ fn annotate_struct_field(

None
}

/// If possible, suggest replacing `ref` with `ref mut`.
fn suggest_ref_mut(tcx: TyCtxt<'_>, binding_span: Span) -> Option<(String)> {
let hi_src = tcx.sess.source_map().span_to_snippet(binding_span).unwrap();
if hi_src.starts_with("ref")
&& hi_src["ref".len()..].starts_with(Pattern_White_Space)
{
let replacement = format!("ref mut{}", &hi_src["ref".len()..]);
Some(replacement)
} else {
None
}
}
17 changes: 0 additions & 17 deletions src/librustc_mir/util/mod.rs
@@ -1,7 +1,3 @@
use core::unicode::property::Pattern_White_Space;
use rustc::ty::TyCtxt;
use syntax_pos::Span;

pub mod aggregate;
pub mod borrowck_errors;
pub mod elaborate_drops;
Expand All @@ -19,16 +15,3 @@ pub use self::alignment::is_disaligned;
pub use self::pretty::{dump_enabled, dump_mir, write_mir_pretty, PassWhere};
pub use self::graphviz::{graphviz_safe_def_name, write_mir_graphviz};
pub use self::graphviz::write_node_label as write_graphviz_node_label;

/// If possible, suggest replacing `ref` with `ref mut`.
pub fn suggest_ref_mut(tcx: TyCtxt<'_>, binding_span: Span) -> Option<(String)> {
let hi_src = tcx.sess.source_map().span_to_snippet(binding_span).unwrap();
if hi_src.starts_with("ref")
&& hi_src["ref".len()..].starts_with(Pattern_White_Space)
{
let replacement = format!("ref mut{}", &hi_src["ref".len()..]);
Some(replacement)
} else {
None
}
}

0 comments on commit abfd4d1

Please sign in to comment.