1
Fork 0

Shift padding out of suggestions for format strings

This commit is contained in:
Mark Rousskov 2019-06-06 16:49:51 -06:00
parent dc13072b7b
commit a859440092
2 changed files with 10 additions and 10 deletions

View file

@ -1043,7 +1043,9 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt<'_>,
let mut show_doc_note = false; let mut show_doc_note = false;
let mut suggestions = vec![]; let mut suggestions = vec![];
for sub in foreign::$kind::iter_subs(fmt_str) { // account for `"` and account for raw strings `r#`
let padding = str_style.map(|i| i + 2).unwrap_or(1);
for sub in foreign::$kind::iter_subs(fmt_str, padding) {
let trn = match sub.translate() { let trn = match sub.translate() {
Some(trn) => trn, Some(trn) => trn,
@ -1064,9 +1066,7 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt<'_>,
} }
if let Some((start, end)) = pos { if let Some((start, end)) = pos {
// account for `"` and account for raw strings `r#` let sp = fmt_sp.from_inner_byte_pos(start, end);
let padding = str_style.map(|i| i + 2).unwrap_or(1);
let sp = fmt_sp.from_inner_byte_pos(start + padding, end + padding);
suggestions.push((sp, trn)); suggestions.push((sp, trn));
} else { } else {
diag.help(&format!("`{}` should be written as `{}`", sub, trn)); diag.help(&format!("`{}` should be written as `{}`", sub, trn));

View file

@ -263,10 +263,10 @@ pub mod printf {
} }
/// Returns an iterator over all substitutions in a given string. /// Returns an iterator over all substitutions in a given string.
pub fn iter_subs(s: &str) -> Substitutions<'_> { pub fn iter_subs(s: &str, start_pos: usize) -> Substitutions<'_> {
Substitutions { Substitutions {
s, s,
pos: 0, pos: start_pos,
} }
} }
@ -711,7 +711,7 @@ pub mod printf {
#[test] #[test]
fn test_iter() { fn test_iter() {
let s = "The %d'th word %% is: `%.*s` %!\n"; let s = "The %d'th word %% is: `%.*s` %!\n";
let subs: Vec<_> = iter_subs(s).map(|sub| sub.translate()).collect(); let subs: Vec<_> = iter_subs(s, 0).map(|sub| sub.translate()).collect();
assert_eq!( assert_eq!(
subs.iter().map(|ms| ms.as_ref().map(|s| &s[..])).collect::<Vec<_>>(), subs.iter().map(|ms| ms.as_ref().map(|s| &s[..])).collect::<Vec<_>>(),
vec![Some("{}"), None, Some("{:.*}"), None] vec![Some("{}"), None, Some("{:.*}"), None]
@ -804,10 +804,10 @@ pub mod shell {
} }
/// Returns an iterator over all substitutions in a given string. /// Returns an iterator over all substitutions in a given string.
pub fn iter_subs(s: &str) -> Substitutions<'_> { pub fn iter_subs(s: &str, start_pos: usize) -> Substitutions<'_> {
Substitutions { Substitutions {
s, s,
pos: 0, pos: start_pos,
} }
} }
@ -940,7 +940,7 @@ pub mod shell {
fn test_iter() { fn test_iter() {
use super::iter_subs; use super::iter_subs;
let s = "The $0'th word $$ is: `$WORD` $!\n"; let s = "The $0'th word $$ is: `$WORD` $!\n";
let subs: Vec<_> = iter_subs(s).map(|sub| sub.translate()).collect(); let subs: Vec<_> = iter_subs(s, 0).map(|sub| sub.translate()).collect();
assert_eq!( assert_eq!(
subs.iter().map(|ms| ms.as_ref().map(|s| &s[..])).collect::<Vec<_>>(), subs.iter().map(|ms| ms.as_ref().map(|s| &s[..])).collect::<Vec<_>>(),
vec![Some("{0}"), None, Some("{WORD}")] vec![Some("{0}"), None, Some("{WORD}")]