mirror of
https://github.com/github/codeql.git
synced 2026-05-01 03:35:13 +02:00
Merge branch 'master' into python-keyword-only-args
This commit is contained in:
@@ -82,8 +82,6 @@ private predicate ordering_method(string name) {
|
||||
private predicate cast_method(string name) {
|
||||
name = "__nonzero__" and major_version() = 2
|
||||
or
|
||||
name = "__bool__"
|
||||
or
|
||||
name = "__int__"
|
||||
or
|
||||
name = "__float__"
|
||||
@@ -118,6 +116,8 @@ predicate preferred_raise(string name, ClassObject ex) {
|
||||
ordering_method(name) and ex = theTypeErrorType()
|
||||
or
|
||||
arithmetic_method(name) and ex = Object::builtin("ArithmeticError")
|
||||
or
|
||||
name = "__bool__" and ex = theTypeErrorType()
|
||||
}
|
||||
|
||||
predicate no_need_to_raise(string name, string message) {
|
||||
|
||||
@@ -25,7 +25,10 @@ predicate safe_method(string name) {
|
||||
name = "values" or
|
||||
name = "iteritems" or
|
||||
name = "iterkeys" or
|
||||
name = "itervalues"
|
||||
name = "itervalues" or
|
||||
name = "__contains__" or
|
||||
name = "__getitem__" or
|
||||
name = "__getattribute__"
|
||||
}
|
||||
|
||||
/** Gets the truthiness (non emptyness) of the default of `p` if that value is mutable */
|
||||
|
||||
@@ -490,3 +490,25 @@ class NiceLocationExpr extends @py_expr {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the definition (of kind `kind`) for the expression `use`, if one can be found.
|
||||
*/
|
||||
cached
|
||||
Definition definitionOf(NiceLocationExpr use, string kind) {
|
||||
exists(string f, int l |
|
||||
result = getUniqueDefinition(use) and
|
||||
kind = "Definition" and
|
||||
use.hasLocationInfo(f, l, _, _, _) and
|
||||
// Ignore if the definition is on the same line as the use
|
||||
not result.getLocation().hasLocationInfo(f, l, _, _, _)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an appropriately encoded version of a filename `name`
|
||||
* passed by the VS Code extension in order to coincide with the
|
||||
* output of `.getFile()` on locatable entities.
|
||||
*/
|
||||
cached
|
||||
File getEncodedFile(string name) { result.getAbsolutePath().replaceAll(":", "_") = name }
|
||||
|
||||
@@ -8,11 +8,6 @@
|
||||
import python
|
||||
import DefinitionTracking
|
||||
|
||||
from NiceLocationExpr use, Definition defn, string kind, string f, int l
|
||||
where
|
||||
defn = getUniqueDefinition(use) and
|
||||
kind = "Definition" and
|
||||
use.hasLocationInfo(f, l, _, _, _) and
|
||||
// Ignore if the definition is on the same line as the use
|
||||
not defn.getLocation().hasLocationInfo(f, l, _, _, _)
|
||||
select use, defn, kind
|
||||
from NiceLocationExpr use, Definition defn, string kind
|
||||
where defn = definitionOf(use, kind)
|
||||
select use, defn, kind
|
||||
19
python/ql/src/analysis/LocalDefinitions.ql
Normal file
19
python/ql/src/analysis/LocalDefinitions.ql
Normal file
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* @name Jump-to-definition links
|
||||
* @description Generates use-definition pairs that provide the data
|
||||
* for jump-to-definition in the code viewer.
|
||||
* @kind definitions
|
||||
* @id python/ide-jump-to-definition
|
||||
* @tags ide-contextual-queries/local-definitions
|
||||
*/
|
||||
|
||||
import python
|
||||
import DefinitionTracking
|
||||
|
||||
external string selectedSourceFile();
|
||||
|
||||
from NiceLocationExpr use, Definition defn, string kind, string f
|
||||
where defn = definitionOf(use, kind)
|
||||
and use.hasLocationInfo(f, _, _, _, _)
|
||||
and getEncodedFile(selectedSourceFile()).getAbsolutePath() = f
|
||||
select use, defn, kind
|
||||
18
python/ql/src/analysis/LocalReferences.ql
Normal file
18
python/ql/src/analysis/LocalReferences.ql
Normal file
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* @name Find-references links
|
||||
* @description Generates use-definition pairs that provide the data
|
||||
* for find-references in the code viewer.
|
||||
* @kind definitions
|
||||
* @id python/ide-find-references
|
||||
* @tags ide-contextual-queries/local-references
|
||||
*/
|
||||
|
||||
import python
|
||||
import DefinitionTracking
|
||||
|
||||
external string selectedSourceFile();
|
||||
|
||||
from NiceLocationExpr use, Definition defn, string kind
|
||||
where defn = definitionOf(use, kind)
|
||||
and defn.getLocation().getFile() = getEncodedFile(selectedSourceFile())
|
||||
select use, defn, kind
|
||||
@@ -2,3 +2,8 @@
|
||||
- qlpack: codeql-python
|
||||
- apply: lgtm-selectors.yml
|
||||
from: codeql-suite-helpers
|
||||
# These are only for IDE use.
|
||||
- exclude:
|
||||
tags contain:
|
||||
- ide-contextual-queries/local-definitions
|
||||
- ide-contextual-queries/local-references
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
| protocols.py:98:5:98:33 | Function __getitem__ | Function always raises $@; raise LookupError instead | file://:Compiled Code:0:0:0:0 | builtin-class ZeroDivisionError | builtin-class ZeroDivisionError |
|
||||
| protocols.py:101:5:101:26 | Function __getattr__ | Function always raises $@; raise AttributeError instead | file://:Compiled Code:0:0:0:0 | builtin-class ZeroDivisionError | builtin-class ZeroDivisionError |
|
||||
| protocols.py:104:5:104:23 | Function __bool__ | Function always raises $@; raise TypeError instead | file://:Compiled Code:0:0:0:0 | builtin-class ZeroDivisionError | builtin-class ZeroDivisionError |
|
||||
|
||||
@@ -5,5 +5,5 @@
|
||||
| om_test.py:71:5:71:19 | Function WrongSpecials.__repr__ | Too few parameters for special method __repr__, which has no parameters, but should have 1, in class $@. | om_test.py:57:1:57:28 | class WrongSpecials | WrongSpecials |
|
||||
| om_test.py:74:5:74:46 | Function WrongSpecials.__add__ | 1 default values(s) will never be used for special method __add__, in class $@. | om_test.py:57:1:57:28 | class WrongSpecials | WrongSpecials |
|
||||
| om_test.py:97:15:97:34 | Function NotOKSpecials.lambda | Too few parameters for special method __sub__, which has 1 parameter, but should have 2, in class $@. | om_test.py:95:1:95:28 | class NotOKSpecials | NotOKSpecials |
|
||||
| protocols.py:104:1:104:12 | Function f | Too few parameters for special method __add__, which has 1 parameter, but should have 2, in class $@. | protocols.py:107:1:107:29 | class MissingMethods | MissingMethods |
|
||||
| protocols.py:104:1:104:12 | Function f | Too few parameters for special method __set__, which has 1 parameter, but should have 3, in class $@. | protocols.py:107:1:107:29 | class MissingMethods | MissingMethods |
|
||||
| protocols.py:107:1:107:12 | Function f | Too few parameters for special method __add__, which has 1 parameter, but should have 2, in class $@. | protocols.py:110:1:110:29 | class MissingMethods | MissingMethods |
|
||||
| protocols.py:107:1:107:12 | Function f | Too few parameters for special method __set__, which has 1 parameter, but should have 3, in class $@. | protocols.py:110:1:110:29 | class MissingMethods | MissingMethods |
|
||||
|
||||
@@ -193,3 +193,8 @@ def list_default(x=[1,2,3,4]):
|
||||
|
||||
def tuple_default(x=(1,2)):
|
||||
do_stuff_based_on_type(x)
|
||||
|
||||
# Modification of parameter with default (safe method)
|
||||
|
||||
def safe_method(x=[]):
|
||||
return x.count(42)
|
||||
|
||||
@@ -101,6 +101,9 @@ class IncorrectSpecialMethods(object):
|
||||
def __getattr__(self):
|
||||
raise ZeroDivisionError()
|
||||
|
||||
def __bool__(self):
|
||||
raise ZeroDivisionError()
|
||||
|
||||
def f(self):
|
||||
pass
|
||||
|
||||
@@ -116,3 +119,8 @@ class OK(object):
|
||||
def __call__(self):
|
||||
yield 0
|
||||
raise StopIteration
|
||||
|
||||
# __bool__ returns `True` by default, so raising `TypeError` should not give an alert
|
||||
# FP reported in https://github.com/github/codeql/issues/2388
|
||||
def __bool__(self):
|
||||
raise TypeError
|
||||
Reference in New Issue
Block a user