Skip to content

Instantly share code, notes, and snippets.

@mallamanis
Created April 30, 2020 17:14
Show Gist options
  • Save mallamanis/c85035ed73d9f26e93cfb83866777055 to your computer and use it in GitHub Desktop.
Save mallamanis/c85035ed73d9f26e93cfb83866777055 to your computer and use it in GitHub Desktop.
No string parsing for inf/nan
/**
* @name Use math.inf or math.nan
* @description Prefer using math.inf instead of parsing infinity
from a string
* @kind problem
* @tags maintainability
* @problem.severity recommendation
* @sub-severity low
* @precision high
* @id py/no-float-inf
*/
import python
from Call call
where call.getFunc().pointsTo(Value::named("float"))
and exists(StrConst parsedStr | parsedStr = call.getAPositionalArg()
and (
parsedStr.getText().toLowerCase() = "inf"
or parsedStr.getText().toLowerCase() = "-inf"
or parsedStr.getText().toLowerCase() = "+inf"
or parsedStr.getText().toLowerCase() = "infinity"
or parsedStr.getText().toLowerCase() = "+infinity"
or parsedStr.getText().toLowerCase() = "-infinity"
or parsedStr.getText().toLowerCase() = "nan"
))
select call, "Use `math.inf` or `math.nan` instead of parsing with `float()`."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment