tracks whether a variable _must* carry a constant value at a given point in the program. Semgrep performs constant folding when matching literal patterns. Semgrep can track Boolean, numeric, and string constants. Semgrep AppSec Platform supports interprocedural (cross-function), interfile (cross-file) constant propagation. Semgrep Community Edition (CE) supports intrafile (single-file) constant propagation.Documentation Index
Fetch the complete documentation index at: https://docs.semgrep.dev/llms.txt
Use this file to discover all available pages before exploring further.
metavariable-comparison
Using constant propagation, the metavariable-comparison operator works with any constant variable instead of just literals.
Mutable objects
In general, Semgrep assumes that constant objects are immutable and won’t be modified by function calls. This can lead to false positives, especially in languages where strings are mutable, such as C and Ruby. The only exceptions are method calls whose returning value is ignored. In these cases, Semgrep assumes that the method call may be mutating the object that’s called. This helps reduce false positives in Ruby. For example:REGEX class variable:
Test2 when presented with the following code:
public to private, Semgrep returns a match:
REGEX is public in the first code snippet, Semgrep doesn’t propagate its value to other classes on the assumption that it could have mutated. However, in the second example, Semgrep understands that REGEX is private and only assigned to once. Therefore, Semgrep assumes it is immutable.
The rule would also work with:
Disable constant propagation
You can disable constant propagation on a per-rule basis using ruleoptions: by setting constant_propagation: false.