Skip to content

Commit 29eacbd

Browse files
author
Robert Marsh
committed
Merge branch 'main' into rdmarsh/cpp/use-taint-configuration-dtt
Update for submodule bump
2 parents bd00988 + 8bb9e8a commit 29eacbd

13 files changed

Lines changed: 492 additions & 39 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* Two issues causing the 'Unused local variable' query (`cpp/unused-local-variable`) to produce false positive results have been fixed.

cpp/ql/src/Best Practices/Unused Entities/UnusedLocals.ql

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,12 @@ where
5757
not declarationHasSideEffects(v) and
5858
not exists(AsmStmt s | f = s.getEnclosingFunction()) and
5959
not v.getAnAttribute().getName() = "unused" and
60-
not any(ErrorExpr e).getEnclosingFunction() = f // unextracted expr likely used `v`
60+
not any(ErrorExpr e).getEnclosingFunction() = f and // unextracted expr may use `v`
61+
not exists(
62+
Literal l // this case can be removed when the `myFunction2( [obj](){} );` test case doesn't depend on this exclusion
63+
|
64+
l.getEnclosingFunction() = f and
65+
not exists(l.getValue())
66+
) and
67+
not any(ConditionDeclExpr cde).getEnclosingFunction() = f // this case can be removed when the `if (a = b; a)` test case doesn't depend on this exclusion
6168
select v, "Variable " + v.getName() + " is not used"

cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedLocals/UnusedLocals.expected

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
| code2.cpp:4:6:4:7 | v1 | Variable v1 is not used |
2-
| code2.cpp:6:6:6:7 | v3 | Variable v3 is not used |
3-
| code2.cpp:10:16:10:17 | v7 | Variable v7 is not used |
4-
| code2.cpp:25:16:25:17 | v1 | Variable v1 is not used |
5-
| code2.cpp:26:16:26:17 | v2 | Variable v2 is not used |
6-
| code2.cpp:41:11:41:16 | myVar1 | Variable myVar1 is not used |
7-
| code2.cpp:63:7:63:8 | v3 | Variable v3 is not used |
1+
| code2.cpp:5:6:5:7 | v1 | Variable v1 is not used |
2+
| code2.cpp:7:6:7:7 | v3 | Variable v3 is not used |
3+
| code2.cpp:11:16:11:17 | v7 | Variable v7 is not used |
4+
| code2.cpp:26:16:26:17 | v1 | Variable v1 is not used |
5+
| code2.cpp:27:16:27:17 | v2 | Variable v2 is not used |
6+
| code2.cpp:42:11:42:16 | myVar1 | Variable myVar1 is not used |
7+
| code2.cpp:64:7:64:8 | v3 | Variable v3 is not used |
8+
| code2.cpp:108:11:108:12 | v2 | Variable v2 is not used |
9+
| code2.cpp:128:9:128:9 | b | Variable b is not used |
810
| code.c:10:18:10:18 | y | Variable y is not used |
911
| code.c:11:18:11:18 | z | Variable z is not used |
1012
| code.c:18:7:18:7 | x | Variable x is not used |

cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedLocals/code2.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// semmle-extractor-options: -std=c++17
12

23
int test_const_init()
34
{
@@ -76,3 +77,89 @@ void test_expect()
7677
}
7778
}
7879
}
80+
81+
// ---
82+
83+
template<class T>
84+
class MyContainer
85+
{
86+
public:
87+
struct Iterator {
88+
const T& operator*() const;
89+
bool operator!=(const Iterator &rhs) const;
90+
Iterator operator++();
91+
};
92+
93+
Iterator begin();
94+
Iterator end();
95+
};
96+
97+
void output(int value);
98+
99+
void test_range_based_for()
100+
{
101+
MyContainer<int> myContainer;
102+
103+
for (int v1 : myContainer) // GOOD: v1 is used
104+
{
105+
output(v1);
106+
}
107+
108+
for (int v2 : myContainer) // BAD: v2 is not used
109+
{
110+
}
111+
}
112+
113+
// ---
114+
115+
int test_lambdas1()
116+
{
117+
int a, b, c, d, e; // (b is not used, but is explicitly captured)
118+
auto myLambda = [a, b, &c](int x, int y) -> int // (y is not used, but is a parameter)
119+
{
120+
return a + c + x;
121+
};
122+
123+
return myLambda(d, e);
124+
}
125+
126+
int test_lambdas2()
127+
{
128+
int a, b; // BAD: b is not used
129+
auto myLambda = [=]() -> int // BAD: myLambda is not used [NOT DETECTED] (due to containing a Constructor)
130+
{
131+
return a;
132+
};
133+
134+
return 0;
135+
}
136+
137+
// ---
138+
139+
void test_if_initializer()
140+
{
141+
bool a = false, b = true; // GOOD: a, b are both used
142+
143+
if (a = b; a)
144+
{
145+
// ...
146+
}
147+
}
148+
149+
// ---
150+
151+
class MyObj
152+
{
153+
public:
154+
MyObj();
155+
};
156+
157+
template<class T>
158+
void myFunction2(T t);
159+
160+
void test_captured_contructor()
161+
{
162+
const auto &obj = MyObj(); // GOOD: obj is used
163+
164+
myFunction2( [obj](){} );
165+
}

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/VariableDeclaration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static VariableDeclaration Create(Context cx, ISymbol symbol, AnnotatedTy
1919
var l = LocalVariable.Create(cx, symbol);
2020
l.PopulateManual(ret, isVar);
2121
if (optionalSyntax != null)
22-
TypeMention.Create(cx, optionalSyntax, parent, type);
22+
TypeMention.Create(cx, optionalSyntax, ret, type);
2323
});
2424
return ret;
2525
}

csharp/ql/test/experimental/ir/ir/PrintAst.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,8 @@ foreach.cs:
465465
# 5| 5: [IntLiteral] 6
466466
# 5| 6: [IntLiteral] 7
467467
# 7| 1: [ForeachStmt] foreach (... ... in ...) ...
468-
# 7| -1: [TypeMention] int
469468
# 7| 0: [LocalVariableDeclExpr] Int32 items
469+
# 7| 0: [TypeMention] int
470470
# 7| 1: [LocalVariableAccess] access to local variable a_array
471471
# 8| 2: [BlockStmt] {...}
472472
# 9| 0: [LocalVariableDeclStmt] ... ...;
@@ -725,9 +725,9 @@ isexpr.cs:
725725
# 12| 1: [LocalVariableAccess] access to local variable obj
726726
# 13| 2: [IfStmt] if (...) ...
727727
# 13| 0: [IsExpr] ... is ...
728-
# 13| -1: [TypeMention] Is_A
729728
# 13| 0: [LocalVariableAccess] access to local variable o
730729
# 13| 1: [VariablePatternExpr] Is_A tmp
730+
# 13| 0: [TypeMention] Is_A
731731
# 14| 1: [BlockStmt] {...}
732732
# 15| 0: [LocalVariableDeclStmt] ... ...;
733733
# 15| 0: [LocalVariableDeclAndInitExpr] Int32 res = ...

csharp/ql/test/library-tests/csharp7/PrintAst.expected

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -722,9 +722,9 @@ CSharp7.cs:
722722
# 235| 1: [IfStmt] if (...) ...
723723
# 235| 0: [LogicalAndExpr] ... && ...
724724
# 235| 0: [IsExpr] ... is ...
725-
# 235| -1: [TypeMention] int
726725
# 235| 0: [LocalVariableAccess] access to local variable o
727726
# 235| 1: [VariablePatternExpr] Int32 i1
727+
# 235| 0: [TypeMention] int
728728
# 235| 1: [GTExpr] ... > ...
729729
# 235| 0: [LocalVariableAccess] access to local variable i1
730730
# 235| 1: [IntLiteral] 0
@@ -738,9 +738,9 @@ CSharp7.cs:
738738
# 237| 1: [LocalVariableAccess] access to local variable i1
739739
# 239| 2: [IfStmt] if (...) ...
740740
# 239| 0: [IsExpr] ... is ...
741-
# 239| -1: [TypeMention] string
742741
# 239| 0: [LocalVariableAccess] access to local variable o
743742
# 239| 1: [VariablePatternExpr] String s1
743+
# 239| 0: [TypeMention] string
744744
# 240| 1: [BlockStmt] {...}
745745
# 241| 0: [ExprStmt] ...;
746746
# 241| 0: [MethodCall] call to method WriteLine
@@ -774,9 +774,9 @@ CSharp7.cs:
774774
# 256| 4: [ConstCase] case ...:
775775
# 256| 0: [ConstantPatternExpr,StringLiteral] "x"
776776
# 256| 1: [IsExpr] ... is ...
777-
# 256| -1: [TypeMention] string
778777
# 256| 0: [LocalVariableAccess] access to local variable o
779778
# 256| 1: [VariablePatternExpr] String s4
779+
# 256| 0: [TypeMention] string
780780
# 257| 5: [ExprStmt] ...;
781781
# 257| 0: [MethodCall] call to method WriteLine
782782
# 257| -1: [TypeAccess] access to type Console
@@ -786,8 +786,8 @@ CSharp7.cs:
786786
# 257| 1: [LocalVariableAccess] access to local variable s4
787787
# 258| 6: [BreakStmt] break;
788788
# 259| 7: [CaseStmt] case ...:
789-
# 259| -1: [TypeMention] int
790789
# 259| 0: [VariablePatternExpr] Int32 i2
790+
# 259| 0: [TypeMention] int
791791
# 259| 1: [GTExpr] ... > ...
792792
# 259| 0: [LocalVariableAccess] access to local variable i2
793793
# 259| 1: [IntLiteral] 0
@@ -800,8 +800,8 @@ CSharp7.cs:
800800
# 260| 1: [LocalVariableAccess] access to local variable i2
801801
# 261| 9: [BreakStmt] break;
802802
# 262| 10: [CaseStmt] case ...:
803-
# 262| -1: [TypeMention] int
804803
# 262| 0: [VariablePatternExpr] Int32 i3
804+
# 262| 0: [TypeMention] int
805805
# 263| 11: [ExprStmt] ...;
806806
# 263| 0: [MethodCall] call to method WriteLine
807807
# 263| -1: [TypeAccess] access to type Console
@@ -811,8 +811,8 @@ CSharp7.cs:
811811
# 263| 1: [LocalVariableAccess] access to local variable i3
812812
# 264| 12: [BreakStmt] break;
813813
# 265| 13: [CaseStmt] case ...:
814-
# 265| -1: [TypeMention] string
815814
# 265| 0: [VariablePatternExpr] String s2
815+
# 265| 0: [TypeMention] string
816816
# 266| 14: [ExprStmt] ...;
817817
# 266| 0: [MethodCall] call to method WriteLine
818818
# 266| -1: [TypeAccess] access to type Console
@@ -898,9 +898,9 @@ CSharp7.cs:
898898
# 299| 0: [LocalVariableAccess] access to local variable x
899899
# 299| 1: [IntLiteral] 10
900900
# 299| 1: [IsExpr] ... is ...
901-
# 299| -1: [TypeMention] int
902901
# 299| 0: [LocalVariableAccess] access to local variable x
903902
# 299| 1: [VariablePatternExpr] Int32 y
903+
# 299| 0: [TypeMention] int
904904
# 299| 1: [PreIncrExpr] ++...
905905
# 299| 0: [LocalVariableAccess] access to local variable x
906906
# 300| 2: [BlockStmt] {...}

csharp/ql/test/library-tests/csharp8/PrintAst.expected

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ AsyncStreams.cs:
3636
# 17| -1: [TypeMention] Void
3737
# 18| 4: [BlockStmt] {...}
3838
# 19| 0: [ForeachStmt] foreach (... ... in ...) ...
39-
# 19| -1: [TypeMention] int
4039
# 19| 0: [LocalVariableDeclExpr] Int32 item
40+
# 19| 0: [TypeMention] int
4141
# 19| 1: [MethodCall] call to method Items
4242
# 20| 2: [ExprStmt] ...;
4343
# 20| 0: [MethodCall] call to method WriteLine
@@ -744,9 +744,9 @@ patterns.cs:
744744
# 7| 1: [IntLiteral] 2
745745
# 9| 1: [IfStmt] if (...) ...
746746
# 9| 0: [IsExpr] ... is ...
747-
# 9| -1: [TypeMention] MyStruct
748747
# 9| 0: [LocalVariableAccess] access to local variable o
749748
# 9| 1: [VariablePatternExpr] MyStruct ms1
749+
# 9| 0: [TypeMention] MyStruct
750750
# 10| 1: [BlockStmt] {...}
751751
# 13| 2: [IfStmt] if (...) ...
752752
# 13| 0: [LogicalAndExpr] ... && ...
@@ -758,8 +758,8 @@ patterns.cs:
758758
# 13| 1: [TypeAccess] access to type MyStruct
759759
# 13| 0: [TypeMention] MyStruct
760760
# 13| 3: [PropertyPatternExpr] { ... }
761-
# 13| -1: [TypeMention] int
762761
# 13| 0: [LabeledPatternExpr,VariablePatternExpr] Int32 x
762+
# 13| 0: [TypeMention] int
763763
# 13| 1: [LTExpr] ... < ...
764764
# 13| 0: [LocalVariableAccess] access to local variable x
765765
# 13| 1: [IntLiteral] 4
@@ -818,8 +818,8 @@ patterns.cs:
818818
# 36| 1: [SwitchStmt] switch (...) {...}
819819
# 36| 0: [LocalVariableAccess] access to local variable s
820820
# 38| 0: [CaseStmt] case ...:
821-
# 38| -1: [TypeMention] MyStruct
822821
# 38| 0: [VariablePatternExpr] MyStruct ms1
822+
# 38| 0: [TypeMention] MyStruct
823823
# 38| 1: [EQExpr] ... == ...
824824
# 38| 0: [FieldAccess] access to field X
825825
# 38| -1: [LocalVariableAccess] access to local variable ms1
@@ -831,8 +831,8 @@ patterns.cs:
831831
# 39| 0: [StringLiteral] "Hit the breakpoint"
832832
# 40| 2: [BreakStmt] break;
833833
# 41| 3: [CaseStmt] case ...:
834-
# 41| -1: [TypeMention] MyStruct
835834
# 41| 0: [VariablePatternExpr] MyStruct ms2
835+
# 41| 0: [TypeMention] MyStruct
836836
# 41| 1: [LTExpr] ... < ...
837837
# 41| 0: [FieldAccess] access to field X
838838
# 41| -1: [LocalVariableAccess] access to local variable ms2
@@ -850,8 +850,8 @@ patterns.cs:
850850
# 48| 1: [TypeAccess] access to type MyStruct
851851
# 48| 0: [TypeMention] MyStruct
852852
# 48| 3: [PropertyPatternExpr] { ... }
853-
# 48| -1: [TypeMention] int
854853
# 48| 0: [LabeledPatternExpr,VariablePatternExpr] Int32 x
854+
# 48| 0: [TypeMention] int
855855
# 48| 1: [GTExpr] ... > ...
856856
# 48| 0: [LocalVariableAccess] access to local variable x
857857
# 48| 1: [IntLiteral] 2
@@ -877,8 +877,8 @@ patterns.cs:
877877
# 54| 6: [CaseStmt] case ...:
878878
# 54| 0: [RecursivePatternExpr] { ... }
879879
# 54| 3: [PropertyPatternExpr] { ... }
880-
# 54| -1: [TypeMention] int
881880
# 54| 0: [LabeledPatternExpr,VariablePatternExpr] Int32 x2
881+
# 54| 0: [TypeMention] int
882882
# 54| 1: [GTExpr] ... > ...
883883
# 54| 0: [LocalVariableAccess] access to local variable x2
884884
# 54| 1: [IntLiteral] 2
@@ -908,8 +908,8 @@ patterns.cs:
908908
# 67| 1: [TypeAccess] access to type MyStruct
909909
# 67| 0: [TypeMention] MyStruct
910910
# 67| 3: [PropertyPatternExpr] { ... }
911-
# 67| -1: [TypeMention] int
912911
# 67| 0: [LabeledPatternExpr,VariablePatternExpr] Int32 x
912+
# 67| 0: [TypeMention] int
913913
# 67| 1: [GTExpr] ... > ...
914914
# 67| 0: [LocalVariableAccess] access to local variable x
915915
# 67| 1: [IntLiteral] 2
@@ -942,10 +942,10 @@ patterns.cs:
942942
# 78| 0: [CaseStmt] case ...:
943943
# 78| 0: [RecursivePatternExpr] { ... }
944944
# 78| 2: [PositionalPatternExpr] ( ... )
945-
# 78| -1: [TypeMention] int
946-
# 78| -1: [TypeMention] float
947945
# 78| 0: [VariablePatternExpr] Int32 x
946+
# 78| 0: [TypeMention] int
948947
# 78| 1: [VariablePatternExpr] Single y
948+
# 78| 0: [TypeMention] float
949949
# 78| 1: [LTExpr] ... < ...
950950
# 78| 0: [CastExpr] (...) ...
951951
# 78| 1: [LocalVariableAccess] access to local variable x
@@ -998,8 +998,8 @@ patterns.cs:
998998
# 100| 1: [SwitchExpr] ... switch { ... }
999999
# 100| -1: [ParameterAccess] access to parameter x
10001000
# 101| 0: [SwitchCaseExpr] ... => ...
1001-
# 101| -1: [TypeMention] int
10021001
# 101| 0: [VariablePatternExpr] Int32 y
1002+
# 101| 0: [TypeMention] int
10031003
# 101| 1: [GTExpr] ... > ...
10041004
# 101| 0: [LocalVariableAccess] access to local variable y
10051005
# 101| 1: [IntLiteral] 10
@@ -1101,8 +1101,8 @@ patterns.cs:
11011101
# 128| 1: [TypeAccess] access to type MyStruct
11021102
# 128| 0: [TypeMention] MyStruct
11031103
# 128| 3: [PropertyPatternExpr] { ... }
1104-
# 128| -1: [TypeMention] int
11051104
# 128| 0: [LabeledPatternExpr,VariablePatternExpr] Int32 x
1105+
# 128| 0: [TypeMention] int
11061106
# 128| 1: [GTExpr] ... > ...
11071107
# 128| 0: [LocalVariableAccess] access to local variable x
11081108
# 128| 1: [IntLiteral] 2
@@ -1142,8 +1142,8 @@ patterns.cs:
11421142
# 139| 0: [ConstantPatternExpr,IntLiteral] 2
11431143
# 139| 2: [IntLiteral] 3
11441144
# 140| 2: [SwitchCaseExpr] ... => ...
1145-
# 140| -1: [TypeMention] object
11461145
# 140| 0: [VariablePatternExpr] Object y
1146+
# 140| 0: [TypeMention] object
11471147
# 140| 1: [IsExpr] ... is ...
11481148
# 140| 0: [LocalVariableAccess] access to local variable y
11491149
# 140| 1: [RecursivePatternExpr] { ... }

csharp/ql/test/library-tests/definitions/PrintAst.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ definitions.cs:
204204
# 86| 0: [TypeMention] Exception
205205
# 87| 1: [BlockStmt] {...}
206206
# 88| 0: [ForeachStmt] foreach (... ... in ...) ...
207-
# 88| -1: [TypeMention] S1
208207
# 88| 0: [LocalVariableDeclExpr] S1 s
208+
# 88| 0: [TypeMention] S1
209209
# 88| 1: [ParameterAccess] access to parameter ss
210210
# 89| 2: [BlockStmt] {...}
211211
# 92| 1: [LocalVariableDeclStmt] ... ...;
@@ -507,18 +507,18 @@ definitions.cs:
507507
# 210| 1: [NullLiteral] null
508508
# 211| 1: [IfStmt] if (...) ...
509509
# 211| 0: [IsExpr] ... is ...
510-
# 211| -1: [TypeMention] C8
511510
# 211| 0: [LocalVariableAccess] access to local variable c8a
512511
# 211| 1: [VariablePatternExpr] C8 c8b
512+
# 211| 0: [TypeMention] C8
513513
# 212| 1: [ExprStmt] ...;
514514
# 212| 0: [AssignExpr] ... = ...
515515
# 212| 0: [LocalVariableAccess] access to local variable c8a
516516
# 212| 1: [LocalVariableAccess] access to local variable c8b
517517
# 213| 2: [SwitchStmt] switch (...) {...}
518518
# 213| 0: [LocalVariableAccess] access to local variable c8a
519519
# 215| 0: [CaseStmt] case ...:
520-
# 215| -1: [TypeMention] C8
521520
# 215| 0: [VariablePatternExpr] C8 c8c
521+
# 215| 0: [TypeMention] C8
522522
# 215| 1: [NEExpr] ... != ...
523523
# 215| 0: [LocalVariableAccess] access to local variable c8c
524524
# 215| 1: [NullLiteral] null

0 commit comments

Comments
 (0)