From 431586bb7016311f6dafbc93aee815d406472128 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Fri, 7 Mar 2025 10:53:01 +0100 Subject: [PATCH 1/6] C#: Make the same folder structure for BMN tests as used for traced extractor tests. --- .../{ => Likely Bugs}/IncomparableEquals/IncomparableEquals.cs | 0 .../IncomparableEquals/IncomparableEquals.expected | 0 .../{ => Likely Bugs}/IncomparableEquals/IncomparableEquals.qlref | 0 .../standalone/{ => Likely Bugs}/IncomparableEquals/options | 0 .../{ => Likely Bugs}/ObjectComparison/ObjectComparison.cs | 0 .../{ => Likely Bugs}/ObjectComparison/ObjectComparison.expected | 0 .../{ => Likely Bugs}/ObjectComparison/ObjectComparison.qlref | 0 .../standalone/{ => Likely Bugs}/ObjectComparison/options | 0 8 files changed, 0 insertions(+), 0 deletions(-) rename csharp/ql/test/query-tests/standalone/{ => Likely Bugs}/IncomparableEquals/IncomparableEquals.cs (100%) rename csharp/ql/test/query-tests/standalone/{ => Likely Bugs}/IncomparableEquals/IncomparableEquals.expected (100%) rename csharp/ql/test/query-tests/standalone/{ => Likely Bugs}/IncomparableEquals/IncomparableEquals.qlref (100%) rename csharp/ql/test/query-tests/standalone/{ => Likely Bugs}/IncomparableEquals/options (100%) rename csharp/ql/test/query-tests/standalone/{ => Likely Bugs}/ObjectComparison/ObjectComparison.cs (100%) rename csharp/ql/test/query-tests/standalone/{ => Likely Bugs}/ObjectComparison/ObjectComparison.expected (100%) rename csharp/ql/test/query-tests/standalone/{ => Likely Bugs}/ObjectComparison/ObjectComparison.qlref (100%) rename csharp/ql/test/query-tests/standalone/{ => Likely Bugs}/ObjectComparison/options (100%) diff --git a/csharp/ql/test/query-tests/standalone/IncomparableEquals/IncomparableEquals.cs b/csharp/ql/test/query-tests/standalone/Likely Bugs/IncomparableEquals/IncomparableEquals.cs similarity index 100% rename from csharp/ql/test/query-tests/standalone/IncomparableEquals/IncomparableEquals.cs rename to csharp/ql/test/query-tests/standalone/Likely Bugs/IncomparableEquals/IncomparableEquals.cs diff --git a/csharp/ql/test/query-tests/standalone/IncomparableEquals/IncomparableEquals.expected b/csharp/ql/test/query-tests/standalone/Likely Bugs/IncomparableEquals/IncomparableEquals.expected similarity index 100% rename from csharp/ql/test/query-tests/standalone/IncomparableEquals/IncomparableEquals.expected rename to csharp/ql/test/query-tests/standalone/Likely Bugs/IncomparableEquals/IncomparableEquals.expected diff --git a/csharp/ql/test/query-tests/standalone/IncomparableEquals/IncomparableEquals.qlref b/csharp/ql/test/query-tests/standalone/Likely Bugs/IncomparableEquals/IncomparableEquals.qlref similarity index 100% rename from csharp/ql/test/query-tests/standalone/IncomparableEquals/IncomparableEquals.qlref rename to csharp/ql/test/query-tests/standalone/Likely Bugs/IncomparableEquals/IncomparableEquals.qlref diff --git a/csharp/ql/test/query-tests/standalone/IncomparableEquals/options b/csharp/ql/test/query-tests/standalone/Likely Bugs/IncomparableEquals/options similarity index 100% rename from csharp/ql/test/query-tests/standalone/IncomparableEquals/options rename to csharp/ql/test/query-tests/standalone/Likely Bugs/IncomparableEquals/options diff --git a/csharp/ql/test/query-tests/standalone/ObjectComparison/ObjectComparison.cs b/csharp/ql/test/query-tests/standalone/Likely Bugs/ObjectComparison/ObjectComparison.cs similarity index 100% rename from csharp/ql/test/query-tests/standalone/ObjectComparison/ObjectComparison.cs rename to csharp/ql/test/query-tests/standalone/Likely Bugs/ObjectComparison/ObjectComparison.cs diff --git a/csharp/ql/test/query-tests/standalone/ObjectComparison/ObjectComparison.expected b/csharp/ql/test/query-tests/standalone/Likely Bugs/ObjectComparison/ObjectComparison.expected similarity index 100% rename from csharp/ql/test/query-tests/standalone/ObjectComparison/ObjectComparison.expected rename to csharp/ql/test/query-tests/standalone/Likely Bugs/ObjectComparison/ObjectComparison.expected diff --git a/csharp/ql/test/query-tests/standalone/ObjectComparison/ObjectComparison.qlref b/csharp/ql/test/query-tests/standalone/Likely Bugs/ObjectComparison/ObjectComparison.qlref similarity index 100% rename from csharp/ql/test/query-tests/standalone/ObjectComparison/ObjectComparison.qlref rename to csharp/ql/test/query-tests/standalone/Likely Bugs/ObjectComparison/ObjectComparison.qlref diff --git a/csharp/ql/test/query-tests/standalone/ObjectComparison/options b/csharp/ql/test/query-tests/standalone/Likely Bugs/ObjectComparison/options similarity index 100% rename from csharp/ql/test/query-tests/standalone/ObjectComparison/options rename to csharp/ql/test/query-tests/standalone/Likely Bugs/ObjectComparison/options From 5c0fc1f75badbbcc62f3ae360e8b40c3948f585a Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Fri, 7 Mar 2025 13:20:03 +0100 Subject: [PATCH 2/6] C#: Add BMN tests for cs/constant-condition. --- .../ConstantCondition/ConstantCondition.cs | 27 +++++++++++++++++++ .../ConstantCondition.expected | 4 +++ .../ConstantCondition/ConstantCondition.qlref | 2 ++ .../Control-Flow/ConstantCondition/options | 1 + 4 files changed, 34 insertions(+) create mode 100644 csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs create mode 100644 csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected create mode 100644 csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.qlref create mode 100644 csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/options diff --git a/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs b/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs new file mode 100644 index 000000000000..6767677dae54 --- /dev/null +++ b/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs @@ -0,0 +1,27 @@ +using System; + +partial class C1 +{ + public C2 Prop { get; set; } +} + +class C2 { } + +class ConstantMatching +{ + void M1() + { + var c1 = new C1(); + if (c1.Prop is int) // $ Alert + { + } + + // Should not be considered a constant condition as + // we don't know anything about D. + var d = new D(); + if (d.Prop is C2) // $ Alert + { + } + } +} + diff --git a/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected b/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected new file mode 100644 index 000000000000..7820823f8e30 --- /dev/null +++ b/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected @@ -0,0 +1,4 @@ +| ConstantCondition.cs:15:13:15:26 | ... is ... | Condition always evaluates to 'false'. | +| ConstantCondition.cs:15:24:15:26 | access to type Int32 | Pattern never matches. | +| ConstantCondition.cs:22:13:22:24 | ... is ... | Condition always evaluates to 'false'. | +| ConstantCondition.cs:22:23:22:24 | access to type C2 | Pattern never matches. | diff --git a/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.qlref b/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.qlref new file mode 100644 index 000000000000..6692217230e0 --- /dev/null +++ b/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.qlref @@ -0,0 +1,2 @@ +query: Bad Practices/Control-Flow/ConstantCondition.ql +postprocess: utils/test/InlineExpectationsTestQuery.ql diff --git a/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/options b/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/options new file mode 100644 index 000000000000..7ba3811b2afb --- /dev/null +++ b/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/options @@ -0,0 +1 @@ +semmle-extractor-options: --standalone From b5ea34fcf3e384523866f50613cf3cbdfb47e4f5 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Wed, 5 Mar 2025 11:14:23 +0100 Subject: [PATCH 3/6] C#: We can't do any reasoning based on unknown types on whether they match in a pattern. --- .../lib/semmle/code/csharp/controlflow/internal/Completion.qll | 2 ++ 1 file changed, 2 insertions(+) diff --git a/csharp/ql/lib/semmle/code/csharp/controlflow/internal/Completion.qll b/csharp/ql/lib/semmle/code/csharp/controlflow/internal/Completion.qll index a470d0c4b8a2..6fed45cdf84d 100644 --- a/csharp/ql/lib/semmle/code/csharp/controlflow/internal/Completion.qll +++ b/csharp/ql/lib/semmle/code/csharp/controlflow/internal/Completion.qll @@ -293,6 +293,8 @@ private predicate isMatchingConstant(PatternExpr pe, boolean value) { value = true or exists(Type t, Type strippedType | + not t instanceof UnknownType and + not strippedType instanceof UnknownType and typePatternMustHaveMatchingCompletion(pe, t, strippedType) and not typePatternCommonSubType(t, strippedType) and value = false From 3903a90a11d88070eae2a58ae893b691736cba60 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Fri, 7 Mar 2025 13:22:03 +0100 Subject: [PATCH 4/6] C#: Update expected test output. --- .../Control-Flow/ConstantCondition/ConstantCondition.cs | 2 +- .../Control-Flow/ConstantCondition/ConstantCondition.expected | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs b/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs index 6767677dae54..6f40759b3e67 100644 --- a/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs +++ b/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs @@ -19,7 +19,7 @@ void M1() // Should not be considered a constant condition as // we don't know anything about D. var d = new D(); - if (d.Prop is C2) // $ Alert + if (d.Prop is C2) { } } diff --git a/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected b/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected index 7820823f8e30..25ed39bebb72 100644 --- a/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected +++ b/csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected @@ -1,4 +1,2 @@ | ConstantCondition.cs:15:13:15:26 | ... is ... | Condition always evaluates to 'false'. | | ConstantCondition.cs:15:24:15:26 | access to type Int32 | Pattern never matches. | -| ConstantCondition.cs:22:13:22:24 | ... is ... | Condition always evaluates to 'false'. | -| ConstantCondition.cs:22:23:22:24 | access to type C2 | Pattern never matches. | From a16c2c80f751c7d280ecd9cac966cd04574a71ad Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Mon, 10 Mar 2025 13:03:01 +0100 Subject: [PATCH 5/6] C#: Add change-note. --- .../ql/src/change-notes/2025-03-10-unknown-type-matching.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 csharp/ql/src/change-notes/2025-03-10-unknown-type-matching.md diff --git a/csharp/ql/src/change-notes/2025-03-10-unknown-type-matching.md b/csharp/ql/src/change-notes/2025-03-10-unknown-type-matching.md new file mode 100644 index 000000000000..ac1a88353ddf --- /dev/null +++ b/csharp/ql/src/change-notes/2025-03-10-unknown-type-matching.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* Increase query precision for `cs/useless-assignment-to-local` and `cs/constant-condition` when *unknown* types are involved (mostly relevant for build mode none databases). From 21aa62c18c677e4a970fd071c11084d691877aaa Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Mon, 10 Mar 2025 13:28:20 +0100 Subject: [PATCH 6/6] C#: Address review comments. --- csharp/ql/src/change-notes/2025-03-10-unknown-type-matching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/csharp/ql/src/change-notes/2025-03-10-unknown-type-matching.md b/csharp/ql/src/change-notes/2025-03-10-unknown-type-matching.md index ac1a88353ddf..634f46067007 100644 --- a/csharp/ql/src/change-notes/2025-03-10-unknown-type-matching.md +++ b/csharp/ql/src/change-notes/2025-03-10-unknown-type-matching.md @@ -1,4 +1,4 @@ --- category: minorAnalysis --- -* Increase query precision for `cs/useless-assignment-to-local` and `cs/constant-condition` when *unknown* types are involved (mostly relevant for build mode none databases). +* Increase query precision for `cs/useless-assignment-to-local` and `cs/constant-condition` when *unknown* types are involved (mostly relevant for `build-mode: none` databases).