use crate::linter::{Diagnostic, Fix, LintResult, Severity, Span};
pub fn check(source: &str) -> LintResult {
let mut result = LintResult::new();
if source.trim().is_empty() {
return result;
}
if has_suffixes(source) {
return result;
}
let span = Span::new(1, 1, 1, 1);
let fix_replacement = format!(".SUFFIXES:\n\n{}", source);
let diag = Diagnostic::new(
"MAKE013",
Severity::Warning,
"Missing .SUFFIXES - built-in implicit rules slow down Make (consider adding .SUFFIXES: to disable)",
span,
)
.with_fix(Fix::new(&fix_replacement));
result.add(diag);
result
}
fn has_suffixes(source: &str) -> bool {
for line in source.lines() {
let trimmed = line.trim();
if trimmed.starts_with(".SUFFIXES") {
return true;
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_MAKE013_detects_missing_suffixes() {
let makefile = "all: app\n\napp: main.o\n\tgcc main.o -o app";
let result = check(makefile);
assert_eq!(result.diagnostics.len(), 1);
let diag = &result.diagnostics[0];
assert_eq!(diag.code, "MAKE013");
assert_eq!(diag.severity, Severity::Warning);
assert!(diag.message.to_lowercase().contains("suffixes"));
}
#[test]
fn test_MAKE013_no_warning_with_suffixes() {
let makefile = ".SUFFIXES:\n\nall: app\n\napp: main.o\n\tgcc main.o -o app";
let result = check(makefile);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_MAKE013_provides_fix() {
let makefile = "all: app\n\napp: main.o\n\tgcc main.o -o app";
let result = check(makefile);
assert!(result.diagnostics[0].fix.is_some());
let fix = result.diagnostics[0].fix.as_ref().unwrap();
assert!(fix.replacement.contains(".SUFFIXES:"));
}
#[test]
fn test_MAKE013_detects_in_complex_makefile() {
let makefile = "# Makefile\nCC = gcc\n\nall: app\n\napp: main.o\n\tgcc main.o -o app";
let result = check(makefile);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_MAKE013_no_warning_with_suffixes_anywhere() {
let makefile = "all: app\n\n.SUFFIXES:\n\napp: main.o\n\tgcc main.o -o app";
let result = check(makefile);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_MAKE013_case_sensitive() {
let makefile = ".suffixes:\n\nall: app\n\napp: main.o\n\tgcc main.o -o app";
let result = check(makefile);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_MAKE013_no_warning_with_custom_suffixes() {
let makefile = ".SUFFIXES: .c .o\n\nall: app";
let result = check(makefile);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_MAKE013_empty_makefile() {
let makefile = "";
let result = check(makefile);
assert_eq!(result.diagnostics.len(), 0);
}
}