MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / vacuous_assertion

Function vacuous_assertion

atomic-audit/src/substantive/rust.rs:319–385  ·  view source on GitHub ↗
(
    node: Node<'_>,
    context: &mut Context<'_>,
    issues: &mut Vec<StructuralIssue>,
)

Source from the content-addressed store, hash-verified

317}
318
319fn vacuous_assertion(
320 node: Node<'_>,
321 context: &mut Context<'_>,
322 issues: &mut Vec<StructuralIssue>,
323) -> Result<(), &'static str> {
324 let mut ancestor = node.parent();
325 let mut test = crate::risk::test_support(context.path, "");
326 while let Some(parent) = ancestor {
327 if parent.kind() == "function_item" {
328 test |= is_test(parent, context);
329 break;
330 }
331 ancestor = parent.parent();
332 }
333 if !test {
334 return Ok(());
335 }
336 let Some(name) = node.child_by_field_name("macro") else {
337 return Ok(());
338 };
339 let name = tokens::text(name, context.source)
340 .rsplit("::")
341 .next()
342 .unwrap_or("");
343 let Some(tree) = tokens::named(node)
344 .into_iter()
345 .find(|node| node.kind() == "token_tree")
346 else {
347 return Ok(());
348 };
349 let tokens = context.normalized(tree)?;
350 let values: Vec<_> = tokens.iter().map(|token| token.text.as_str()).collect();
351 let assertion = values.get(1..values.len().saturating_sub(1)).unwrap_or(&[]);
352 let comma = assertion
353 .iter()
354 .position(|token| *token == ",")
355 .unwrap_or(assertion.len());
356 let vacuous = if name == "assert" {
357 assertion[..comma] == ["true"]
358 } else if name == "assert_eq" && comma < assertion.len() {
359 let left = &assertion[..comma];
360 let rest = &assertion[comma + 1..];
361 let end = rest
362 .iter()
363 .position(|token| *token == ",")
364 .unwrap_or(rest.len());
365 let simple = |tokens: &[&str]| {
366 tokens.iter().all(|token| {
367 *token == "."
368 || *token == "::"
369 || token.chars().all(|c| c.is_alphanumeric() || c == '_')
370 })
371 };
372 !left.is_empty() && left == &rest[..end] && simple(left)
373 } else {
374 false
375 };
376 if vacuous {

Callers 1

inspectFunction · 0.85

Calls 15

test_supportFunction · 0.85
is_testFunction · 0.85
parentMethod · 0.80
issueMethod · 0.80
getMethod · 0.65
textFunction · 0.50
namedFunction · 0.50
kindMethod · 0.45
nextMethod · 0.45
into_iterMethod · 0.45
normalizedMethod · 0.45
collectMethod · 0.45

Tested by

no test coverage detected