Skip to content

Commit c1b8c8e

Browse files
{"schema":"decodex/commit/1","summary":"Land Detect serde Option defaults in macro fields","authority":"manual"}
2 parents f509613 + 5d9dcbd commit c1b8c8e

2 files changed

Lines changed: 474 additions & 4 deletions

File tree

src/style.rs

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,8 +1309,11 @@ struct Payload {
13091309
.expect("has ctx");
13101310
let (violations, edits) = crate::style::collect_violations(&ctx, true);
13111311

1312-
assert!(violations.iter().any(|v| v.rule == "RUST-STYLE-SERDE-001" && v.fixable));
1313-
assert!(edits.iter().any(|e| e.rule == "RUST-STYLE-SERDE-001"));
1312+
assert_eq!(
1313+
violations.iter().filter(|v| v.rule == "RUST-STYLE-SERDE-001" && v.fixable).count(),
1314+
1
1315+
);
1316+
assert_eq!(edits.iter().filter(|e| e.rule == "RUST-STYLE-SERDE-001").count(), 1);
13141317

13151318
let mut rewritten = original.to_owned();
13161319
let applied = fixes::apply_edits(&mut rewritten, edits).expect("apply edits");
@@ -1337,8 +1340,11 @@ struct Payload {
13371340
.expect("has ctx");
13381341
let (violations, edits) = crate::style::collect_violations(&ctx, true);
13391342

1340-
assert!(violations.iter().any(|v| v.rule == "RUST-STYLE-SERDE-001" && v.fixable));
1341-
assert!(edits.iter().any(|e| e.rule == "RUST-STYLE-SERDE-001"));
1343+
assert_eq!(
1344+
violations.iter().filter(|v| v.rule == "RUST-STYLE-SERDE-001" && v.fixable).count(),
1345+
1
1346+
);
1347+
assert_eq!(edits.iter().filter(|e| e.rule == "RUST-STYLE-SERDE-001").count(), 1);
13421348

13431349
let mut rewritten = original.to_owned();
13441350
let applied = fixes::apply_edits(&mut rewritten, edits).expect("apply edits");
@@ -1348,6 +1354,87 @@ struct Payload {
13481354
assert!(rewritten.contains(r#"#[serde(rename = "value")]"#));
13491355
}
13501356

1357+
#[test]
1358+
fn serde001_fix_removes_default_attr_inside_field_like_macro_tokens() {
1359+
let original = r#"
1360+
define_payload! {
1361+
struct Payload {
1362+
#[serde(
1363+
default = "outputs::downstream_quality_readback::default_pubfi_scheduled_catalyst_clock_readback"
1364+
)]
1365+
value: Option<String>,
1366+
}
1367+
}
1368+
"#;
1369+
let ctx = shared::read_file_context_from_text(
1370+
Path::new("serde001_macro_option_default.rs"),
1371+
original.to_owned(),
1372+
)
1373+
.expect("context")
1374+
.expect("has ctx");
1375+
let (violations, edits) = crate::style::collect_violations(&ctx, true);
1376+
1377+
assert_eq!(
1378+
violations.iter().filter(|v| v.rule == "RUST-STYLE-SERDE-001" && v.fixable).count(),
1379+
1
1380+
);
1381+
assert_eq!(edits.iter().filter(|e| e.rule == "RUST-STYLE-SERDE-001").count(), 1);
1382+
1383+
let mut rewritten = original.to_owned();
1384+
let applied = fixes::apply_edits(&mut rewritten, edits).expect("apply edits");
1385+
1386+
assert!(applied >= 1);
1387+
assert!(!rewritten.contains("default_pubfi_scheduled_catalyst_clock_readback"));
1388+
assert!(rewritten.contains("value: Option<String>,"));
1389+
}
1390+
1391+
#[test]
1392+
fn serde001_macro_scan_ignores_non_option_field() {
1393+
let original = r#"
1394+
define_payload! {
1395+
struct Payload {
1396+
#[serde(default = "default_value")]
1397+
value: String,
1398+
}
1399+
}
1400+
"#;
1401+
let ctx = shared::read_file_context_from_text(
1402+
Path::new("serde001_macro_non_option_default.rs"),
1403+
original.to_owned(),
1404+
)
1405+
.expect("context")
1406+
.expect("has ctx");
1407+
let (violations, edits) = crate::style::collect_violations(&ctx, true);
1408+
1409+
assert!(!violations.iter().any(|v| v.rule == "RUST-STYLE-SERDE-001"));
1410+
assert!(!edits.iter().any(|e| e.rule == "RUST-STYLE-SERDE-001"));
1411+
}
1412+
1413+
#[test]
1414+
fn serde001_macro_scan_ignores_string_and_comment_text() {
1415+
let original = r##"
1416+
quote_like! {
1417+
"#[serde(default)] value: Option<String>,"
1418+
r#"
1419+
#[serde(default)]
1420+
raw_value: Option<String>,
1421+
"#
1422+
// #[serde(default)] comment_value: Option<String>,
1423+
/* #[serde(default)] block_value: Option<String>, */
1424+
}
1425+
"##;
1426+
let ctx = shared::read_file_context_from_text(
1427+
Path::new("serde001_macro_string_comment_default.rs"),
1428+
original.to_owned(),
1429+
)
1430+
.expect("context")
1431+
.expect("has ctx");
1432+
let (violations, edits) = crate::style::collect_violations(&ctx, true);
1433+
1434+
assert!(!violations.iter().any(|v| v.rule == "RUST-STYLE-SERDE-001"));
1435+
assert!(!edits.iter().any(|e| e.rule == "RUST-STYLE-SERDE-001"));
1436+
}
1437+
13511438
#[test]
13521439
fn import005_fix_rewrites_error_rs_to_fully_qualified_paths() {
13531440
let original = r#"

0 commit comments

Comments
 (0)