use std::fs;
use std::path::Path;
use anyhow::Context;
use anyhow::Result;
use super::constants::RUSTC_FIELD_VIS_REMOVE_SUGGESTION;
use super::constants::RUSTC_LINT_SUGGESTION_PREFIX;
use super::imports::UseFix;
use crate::config::DiagnosticCode;
use crate::reporting::Report;
pub(crate) struct FieldVisibilityFixScan {
pub fixes: Vec<UseFix>,
}
pub(crate) fn scan_from_report(report: &Report) -> Result<FieldVisibilityFixScan> {
let root = Path::new(&report.root);
let mut fixes = Vec::new();
for finding in &report.findings {
if finding.diagnostic_code != DiagnosticCode::FieldVisibilityWiderThanType {
continue;
}
let Some(replacement_visibility) =
parse_replacement_from_suggestion(finding.suggestion.as_deref())
else {
continue;
};
let absolute_path = root.join(&finding.path);
let source = fs::read_to_string(&absolute_path)
.with_context(|| format!("failed to read {}", absolute_path.display()))?;
let Some(line_start) = line_byte_offset(&source, finding.line) else {
continue;
};
let line_end = source[line_start..]
.find('\n')
.map_or(source.len(), |pos| line_start + pos);
let line_text = &source[line_start..line_end];
let column_offset = finding.column.saturating_sub(1);
if column_offset > line_text.len() {
continue;
}
let Some(visibility_len) = visibility_annotation_byte_len(&line_text[column_offset..])
else {
continue;
};
let trailing_whitespace_len = line_text[column_offset + visibility_len..]
.chars()
.take_while(|c| c.is_whitespace() && *c != '\n')
.map(char::len_utf8)
.sum::<usize>();
let absolute_start = line_start + column_offset;
let absolute_end = absolute_start + visibility_len + trailing_whitespace_len;
let replacement_text = if replacement_visibility.is_empty() {
String::new()
} else {
format!("{replacement_visibility} ")
};
fixes.push(UseFix {
path: absolute_path,
start: absolute_start,
end: absolute_end,
replacement: replacement_text,
import_group: None,
});
}
Ok(FieldVisibilityFixScan { fixes })
}
fn parse_replacement_from_suggestion(suggestion: Option<&str>) -> Option<String> {
let text = suggestion?;
if text == RUSTC_FIELD_VIS_REMOVE_SUGGESTION {
return Some(String::new());
}
let rest = text.strip_prefix(RUSTC_LINT_SUGGESTION_PREFIX)?;
let end = rest.find('`')?;
Some(rest[..end].to_string())
}
fn visibility_annotation_byte_len(text: &str) -> Option<usize> {
let rest = text.strip_prefix("pub")?;
let mut chars = rest.char_indices();
match chars.next() {
Some((_, '(')) => {
let close = rest.find(')')?;
Some("pub".len() + close + 1)
},
Some((_, c)) if c.is_whitespace() || c == ':' => Some("pub".len()),
None => Some("pub".len()),
_ => None,
}
}
fn line_byte_offset(source: &str, line: usize) -> Option<usize> {
if line == 0 {
return None;
}
if line == 1 {
return Some(0);
}
source
.match_indices('\n')
.nth(line - 2)
.map(|(pos, _)| pos + 1)
}
#[cfg(test)]
mod tests {
use super::RUSTC_FIELD_VIS_REMOVE_SUGGESTION;
use super::RUSTC_LINT_SUGGESTION_PREFIX;
use super::parse_replacement_from_suggestion;
use super::visibility_annotation_byte_len;
#[test]
fn parses_consider_using_with_pub_crate() {
assert_eq!(
parse_replacement_from_suggestion(Some(&format!(
"{RUSTC_LINT_SUGGESTION_PREFIX}pub(crate)`"
))),
Some("pub(crate)".to_string())
);
}
#[test]
fn parses_consider_using_with_pub_super() {
assert_eq!(
parse_replacement_from_suggestion(Some(&format!(
"{RUSTC_LINT_SUGGESTION_PREFIX}pub(super)`"
))),
Some("pub(super)".to_string())
);
}
#[test]
fn parses_remove_annotation() {
assert_eq!(
parse_replacement_from_suggestion(Some(RUSTC_FIELD_VIS_REMOVE_SUGGESTION)),
Some(String::new())
);
}
#[test]
fn rejects_other_suggestion_text() {
assert_eq!(
parse_replacement_from_suggestion(Some("something else")),
None
);
assert_eq!(parse_replacement_from_suggestion(None), None);
}
#[test]
fn visibility_len_bare_pub() {
assert_eq!(visibility_annotation_byte_len("pub leaked: u32"), Some(3));
}
#[test]
fn visibility_len_pub_crate() {
assert_eq!(visibility_annotation_byte_len("pub(crate) x"), Some(10));
}
#[test]
fn visibility_len_pub_super() {
assert_eq!(visibility_annotation_byte_len("pub(super) name"), Some(10));
}
#[test]
fn visibility_len_pub_in_path() {
assert_eq!(
visibility_annotation_byte_len("pub(in crate::foo::bar) y"),
Some("pub(in crate::foo::bar)".len())
);
}
#[test]
fn visibility_len_no_pub() {
assert_eq!(visibility_annotation_byte_len("private_field: u32"), None);
}
}