Debian Codegraph @

tests/snippets/codeql/redundant_select.txt

raw
1 ---input---
2 /**
3 * @name Redundant Select
4 * @description Writing 'seq.Select(e => e)' or 'from e in seq select e' is redundant.
5 * @kind problem
6 * @problem.severity warning
7 * @precision very-high
8 * @id cs/linq/useless-select
9 * @tags maintainability
10 * language-features
11 * external/cwe/cwe-561
12 */
13
14 import csharp
15 import Linq.Helpers
16
17
18 // From https://github.com/github/codeql/blob/ef9f3835e533f97403401507ebf736eb7518cdb2/csharp/ql/src/Linq/RedundantSelect.ql#L18
19 predicate isIdentityFunction(AnonymousFunctionExpr afe) {
20 afe.getNumberOfParameters() = 1 and
21 afe.getExpressionBody() = afe.getParameter(0).getAnAccess()
22 }
23
24 from SelectCall sc
25 where isIdentityFunction(sc.getFunctionExpr())
26 select sc, "This LINQ selection is redundant and can be removed."
27
28 ---tokens---
29 '/*' Comment.Multiline
30 '*' Comment.Multiline
31 '\n ' Comment.Multiline
32 '*' Comment.Multiline
33 ' @name Redundant Select\n ' Comment.Multiline
34 '*' Comment.Multiline
35 " @description Writing 'seq.Select(e => e)' or 'from e in seq select e' is redundant.\n " Comment.Multiline
36 '*' Comment.Multiline
37 ' @kind problem\n ' Comment.Multiline
38 '*' Comment.Multiline
39 ' @problem.severity warning\n ' Comment.Multiline
40 '*' Comment.Multiline
41 ' @precision very-high\n ' Comment.Multiline
42 '*' Comment.Multiline
43 ' @id cs' Comment.Multiline
44 '/' Comment.Multiline
45 'linq' Comment.Multiline
46 '/' Comment.Multiline
47 'useless-select\n ' Comment.Multiline
48 '*' Comment.Multiline
49 ' @tags maintainability\n ' Comment.Multiline
50 '*' Comment.Multiline
51 ' language-features\n ' Comment.Multiline
52 '*' Comment.Multiline
53 ' external' Comment.Multiline
54 '/' Comment.Multiline
55 'cwe' Comment.Multiline
56 '/' Comment.Multiline
57 'cwe-561\n ' Comment.Multiline
58 '*/' Comment.Multiline
59 '\n\n' Text.Whitespace
60
61 'import' Keyword.Builtin
62 ' ' Text.Whitespace
63 'csharp' Name.Variable
64 '\n' Text.Whitespace
65
66 'import' Keyword.Builtin
67 ' ' Text.Whitespace
68 'Linq' Name.Class
69 '.' Punctuation
70 'Helpers' Name.Class
71 '\n\n\n' Text.Whitespace
72
73 '// From https://github.com/github/codeql/blob/ef9f3835e533f97403401507ebf736eb7518cdb2/csharp/ql/src/Linq/RedundantSelect.ql#L18\n' Comment.Single
74
75 'predicate' Keyword.Builtin
76 ' ' Text.Whitespace
77 'isIdentityFunction' Name.Variable
78 '(' Punctuation
79 'AnonymousFunctionExpr' Name.Class
80 ' ' Text.Whitespace
81 'afe' Name.Variable
82 ')' Punctuation
83 ' ' Text.Whitespace
84 '{' Punctuation
85 '\n ' Text.Whitespace
86 'afe' Name.Variable
87 '.' Punctuation
88 'getNumberOfParameters' Name.Variable
89 '()' Punctuation
90 ' ' Text.Whitespace
91 '=' Operator
92 ' ' Text.Whitespace
93 '1' Literal.Number.Integer
94 ' ' Text.Whitespace
95 'and' Keyword.Builtin
96 '\n ' Text.Whitespace
97 'afe' Name.Variable
98 '.' Punctuation
99 'getExpressionBody' Name.Variable
100 '()' Punctuation
101 ' ' Text.Whitespace
102 '=' Operator
103 ' ' Text.Whitespace
104 'afe' Name.Variable
105 '.' Punctuation
106 'getParameter' Name.Variable
107 '(' Punctuation
108 '0' Literal.Number.Integer
109 ').' Punctuation
110 'getAnAccess' Name.Variable
111 '()' Punctuation
112 '\n' Text.Whitespace
113
114 '}' Punctuation
115 '\n\n' Text.Whitespace
116
117 'from' Keyword.Builtin
118 ' ' Text.Whitespace
119 'SelectCall' Name.Class
120 ' ' Text.Whitespace
121 'sc' Name.Variable
122 '\n' Text.Whitespace
123
124 'where' Keyword.Builtin
125 ' ' Text.Whitespace
126 'isIdentityFunction' Name.Variable
127 '(' Punctuation
128 'sc' Name.Variable
129 '.' Punctuation
130 'getFunctionExpr' Name.Variable
131 '())' Punctuation
132 '\n' Text.Whitespace
133
134 'select' Keyword.Builtin
135 ' ' Text.Whitespace
136 'sc' Name.Variable
137 ',' Punctuation
138 ' ' Text.Whitespace
139 '"This LINQ selection is redundant and can be removed."' Literal.String
140 '\n' Text.Whitespace