C#: Introduce Expr.getIntValue.#21701
Merged
aschackmull merged 1 commit intogithub:mainfrom Apr 14, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a new Expr.getIntValue() helper in the C# QL libraries to replace the common getValue().toInt() pattern, improving performance (via caching) and tightening semantics to only integral/enum-typed expressions.
Changes:
- Added cached
Expr.getIntValue()in the core expression library. - Updated a set of queries/libraries to use
getIntValue()instead ofgetValue().toInt()(and in one place replaced a string comparison"0"with an integer comparison0).
Show a summary per file
| File | Description |
|---|---|
| csharp/ql/lib/semmle/code/csharp/exprs/Expr.qll | Adds the new cached getIntValue() API on Expr. |
| csharp/ql/src/Security Features/InsufficientKeySize.ql | Uses getIntValue() for key-size threshold checks. |
| csharp/ql/src/Likely Bugs/PossibleLossOfPrecision.ql | Uses getIntValue() for exact integer-division detection. |
| csharp/ql/src/Likely Bugs/MishandlingJapaneseEra.ql | Uses getIntValue() for date/era argument checks (including era == 0). |
| csharp/ql/src/Likely Bugs/BadCheckOdd.ql | Uses getIntValue() for parity-check constants and positivity checks. |
| csharp/ql/lib/semmle/code/csharp/frameworks/system/runtime/CompilerServices.qll | Uses getIntValue() for attribute constructor argument extraction. |
| csharp/ql/lib/semmle/code/csharp/dataflow/internal/rangeanalysis/ConstantUtils.qll | Uses getIntValue() for recognizing constant integer expressions. |
| csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll | Replaces local integer-constant parsing with getIntValue(). |
| csharp/ql/lib/semmle/code/csharp/commons/Constants.qll | Uses getIntValue() when computing integral min/max for constant comparisons. |
| csharp/ql/lib/semmle/code/csharp/commons/ComparisonTest.qll | Uses getIntValue() for constant extraction in comparison-test logic. |
| csharp/ql/lib/semmle/code/csharp/Conversion.qll | Uses getIntValue() in constant conversion predicates. |
| csharp/ql/examples/snippets/integer_literal.ql | Updates snippet to demonstrate getIntValue(). |
Copilot's findings
- Files reviewed: 12/12 changed files
- Comments generated: 0
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
I noticed that the
Expr.getValue().toInt()pattern was generating some poor performance due to lots of repeated string operations, so at first I just wanted to cache this to improve performance, but then I noticed that the pattern actually also yields results for string and char literals (e.g."4"and'4'both yield 4), which is very unlikely to be intended in the various uses of this pattern. So besides improving some performance, this may also fix some bugs.