Repository navigation
fix(rust): assert_eq!(matches!(..), true) is only as strong as its pattern (#16290) #16295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+531
−163
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3df1434
fix(rust): assert_eq!(matches!(..), true) is only as strong as its pa…
claude 49e75b3
chore(cache): generation 1.201 for compared pattern oracles (#16290)
claude bf04850
test(fixtures): wildcard_owner_discard reads its side-only compared p…
claude 455d073
fix(rust): compared matches! reads through comments, groups and block…
claude 02662f4
merge main; cache generation 1.202 for compared pattern oracles (#16290)
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
.ripr/traceability.d/16290-matches-bool-equality-strength.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| [[extend]] | ||
| id = "RIPR-SPEC-0231" | ||
| tests = [ | ||
| "crates/ripr/tests/matches_bool_equality_strength.rs::a_matches_compared_with_true_is_only_as_strong_as_its_pattern", | ||
| ] | ||
| code = [ | ||
| "crates/ripr/src/analysis/extract/oracles/pattern_admission.rs", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| <!-- section: Fixed --> | ||
| - `assert_eq!(matches!(value, PATTERN), true)` in Rust tests is now only as strong as its pattern, as `assert!(matches!(value, PATTERN))` already was. A wildcard, side-only (`Some(_)`) or several-value pattern no longer makes a change read `exposed` when it is written as an equality with `true`; a pattern that pins one value stays exact (#16290). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| //! `assert_eq!(matches!(x, PATTERN), true)` asserts the pattern exactly as | ||
| //! `assert!(matches!(x, PATTERN))` does, so it is only as strong as its | ||
| //! pattern (#16290, RIPR-SPEC-0231 rules 1 and 3). The changed boundary moves | ||
| //! `discount(100)` from 100 to 90, so an unpinned pattern passes on both | ||
| //! versions and must not read `exposed`. | ||
|
|
||
| use ripr::{CheckInput, ExposureClass, Mode, OutputFormat, ProbeFamily, check_workspace}; | ||
| use std::path::PathBuf; | ||
| use std::sync::atomic::{AtomicU64, Ordering}; | ||
|
|
||
| const MANIFEST: &str = | ||
| "[package]\nname = \"demo\"\nversion = \"0.0.0\"\nedition = \"2024\"\n\n[workspace]\n"; | ||
|
|
||
| const LIB: &str = "pub fn discount(amount: i32) -> i32 {\n if amount >= 100 { amount - 10 } else { amount }\n}\n"; | ||
|
|
||
| const DIFF: &str = "diff --git a/src/lib.rs b/src/lib.rs\n--- a/src/lib.rs\n+++ b/src/lib.rs\n@@ -1,3 +1,3 @@\n pub fn discount(amount: i32) -> i32 {\n- if amount > 100 { amount - 10 } else { amount }\n+ if amount >= 100 { amount - 10 } else { amount }\n }\n"; | ||
|
|
||
| struct Fixture(PathBuf); | ||
|
|
||
| impl Drop for Fixture { | ||
| fn drop(&mut self) { | ||
| let _ = std::fs::remove_dir_all(&self.0); | ||
| } | ||
| } | ||
|
|
||
| /// The class of the changed predicate when the one boundary test in | ||
| /// `tests/pin.rs` has `body`. | ||
| fn class_with(body: &str) -> Result<ExposureClass, String> { | ||
| static SEQUENCE: AtomicU64 = AtomicU64::new(0); | ||
| let root = std::env::temp_dir().join(format!( | ||
| "ripr-matches-bool-equality-{}-{}", | ||
| std::process::id(), | ||
| SEQUENCE.fetch_add(1, Ordering::Relaxed) | ||
| )); | ||
| // `create_dir` refuses a path that already exists, a symlink included, | ||
| // so the fixture never writes through a directory it did not create. | ||
| std::fs::create_dir(&root) | ||
| .map_err(|error| format!("create fixture root {}: {error}", root.display()))?; | ||
| let fixture = Fixture(root); | ||
| let test = format!("#[test]\nfn discount_boundary() {{\n {body}\n}}\n"); | ||
| for (relative, text) in [ | ||
| ("Cargo.toml", MANIFEST), | ||
| ("src/lib.rs", LIB), | ||
| ("tests/pin.rs", test.as_str()), | ||
| ("diff.patch", DIFF), | ||
| ] { | ||
| let path = fixture.0.join(relative); | ||
| if let Some(parent) = path.parent() { | ||
| std::fs::create_dir_all(parent) | ||
| .map_err(|error| format!("create directory {}: {error}", parent.display()))?; | ||
| } | ||
| std::fs::write(&path, text) | ||
| .map_err(|error| format!("write file {}: {error}", path.display()))?; | ||
| } | ||
| let output = check_workspace(CheckInput { | ||
| root: fixture.0.clone(), | ||
| diff_file: Some(fixture.0.join("diff.patch")), | ||
| mode: Mode::Ready, | ||
| format: OutputFormat::Json, | ||
| include_unchanged_tests: true, | ||
| ..CheckInput::default() | ||
| })?; | ||
| let finding = output | ||
| .findings | ||
| .into_iter() | ||
| .find(|finding| finding.probe.family == ProbeFamily::Predicate) | ||
| .ok_or("missing changed predicate probe")?; | ||
| assert!( | ||
| finding | ||
| .related_tests | ||
| .iter() | ||
| .any(|related| related.name == "discount_boundary"), | ||
| "the boundary test must be the related test: {finding:?}" | ||
| ); | ||
| Ok(finding.class) | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_matches_compared_with_true_is_only_as_strong_as_its_pattern() -> Result<(), String> { | ||
| // Controls: a pinned pattern stays exact in both operand orders, as the | ||
| // `assert!` spelling does. | ||
| for body in [ | ||
| "assert!(matches!(demo::discount(100), 90));", | ||
| "assert_eq!(matches!(demo::discount(100), 90), true);", | ||
| "assert_eq!(true, matches!(demo::discount(100), 90));", | ||
| "assert_eq!({ matches!(demo::discount(100), 90) } /* pinned */, true);", | ||
| ] { | ||
| assert_eq!(class_with(body)?, ExposureClass::Exposed, "{body}"); | ||
| } | ||
| // The `assert!` spelling of an unpinned pattern is already weak. | ||
| assert_eq!( | ||
| class_with("assert!(matches!(demo::discount(100), _));")?, | ||
| ExposureClass::WeaklyExposed | ||
| ); | ||
| for body in [ | ||
| "assert_eq!(matches!(demo::discount(100), _), true);", | ||
| "assert_eq!(matches!(Some(demo::discount(100)), Some(_)), true);", | ||
| "assert_eq!(matches!(demo::discount(100), 90 | 100), true);", | ||
| "assert_eq!(true, matches!(demo::discount(100), 90 | 100));", | ||
| "assert_eq!((matches!(demo::discount(100), _)), (true));", | ||
|
EffortlessSteven marked this conversation as resolved.
Resolved
|
||
| "assert_ne!(matches!(demo::discount(100), _), false);", | ||
| // Comments, groups and a block holding only the operand are | ||
| // transparent. | ||
| "assert_eq!((matches!(demo::discount(100), 90 | 100)) /* either */, true);", | ||
| "assert_eq!({ matches!(demo::discount(100), 90 | 100) }, true);", | ||
| // `assert_ne!` stays an inequality macro whatever the literal, so | ||
| // even a pinned pattern is capped there (rule 1). | ||
| "assert_ne!(matches!(demo::discount(100), 90), false);", | ||
| // A comparison that excludes the pattern stays capped (rule 1). | ||
| "assert_eq!(matches!(demo::discount(100), 90), false);", | ||
| ] { | ||
| assert_eq!( | ||
| class_with(body)?, | ||
| ExposureClass::WeaklyExposed, | ||
| "a compared pattern reads as its pattern: {body}" | ||
| ); | ||
| } | ||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.