Skip to content

Commit 28c0dd9

Browse files
jketemaCopilot
andcommitted
Add maybe_unused regression tests for unused-variable queries
Cover C++17 maybe_unused local and static variables in the unused-variable query tests. Record the current alerts as spurious so the regression cases pass until the queries account for the standard attribute. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 695bfe7 commit 28c0dd9

10 files changed

Lines changed: 98 additions & 25 deletions

File tree

‎MODULE.bazel‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ bazel_dep(name = "gazelle", version = "0.54.0")
3131
bazel_dep(name = "rules_dotnet", version = "0.21.5-codeql.1")
3232
bazel_dep(name = "googletest", version = "1.17.0.bcr.2")
3333
bazel_dep(name = "rules_rust", version = "0.74.0")
34-
bazel_dep(name = "rules_swift", version = "4.0.0-rc5-codeql.3")
34+
bazel_dep(name = "rules_swift", version = "4.0.0-rc5-codeql.2")
3535
bazel_dep(name = "swift-syntax", version = "603.0.2")
3636
bazel_dep(name = "zstd", version = "1.5.7.bcr.1")
3737

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
| code2.cpp:64:7:64:8 | v3 | Variable v3 is not used. |
77
| code2.cpp:108:11:108:12 | v2 | Variable v2 is not used. |
88
| code2.cpp:128:9:128:9 | b | Variable b is not used. |
9+
| code2.cpp:182:23:182:28 | unused | Variable unused is not used. |
910
| code.c:10:18:10:18 | y | Variable y is not used. |
1011
| code.c:11:18:11:18 | z | Variable z is not used. |
1112
| 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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,8 @@ void test_switch_initializer()
176176
// ...
177177
}
178178
}
179+
180+
void test_maybe_unused()
181+
{
182+
[[maybe_unused]] int unused; // $ SPURIOUS: Alert // GOOD: false positive for maybe_unused
183+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
| test.cpp:8:12:8:21 | staticVar6 | Static variable staticVar6 is never read. |
33
| test.cpp:10:11:10:19 | constVar8 | Static variable constVar8 is never read. |
44
| test.cpp:12:12:12:22 | staticVar10 | Static variable staticVar10 is never read. |
5+
| test.cpp:13:29:13:39 | staticVar11 | Static variable staticVar11 is never read. |

‎cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedStaticVariables/test.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
// semmle-extractor-options: -std=c++17
22
int globalVar; // GOOD (not static)
33
static int staticVar1; // GOOD (used)
44
static int staticVar2; // GOOD (used)
@@ -10,6 +10,7 @@ static __attribute__((__unused__)) int staticVar7; // GOOD (unused but this is e
1010
const int constVar8 = 8; // $ Alert // BAD (const defaults to static)
1111
extern const int constVar9 = 9; // GOOD
1212
static int staticVar10 = 10; // $ SPURIOUS: Alert // GOOD [FALSE POSITIVE] (referenced in a never instantiated template)
13+
[[maybe_unused]] static int staticVar11; // $ SPURIOUS: Alert // GOOD (false positive for maybe_unused)
1314

1415
void f()
1516
{

misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.3/MODULE.bazel renamed to misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.2/MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module(
44
name = "rules_swift",
5-
version = "4.0.0-rc5-codeql.3",
5+
version = "4.0.0-rc5-codeql.2",
66
bazel_compatibility = [">=8.0.0"],
77
compatibility_level = 3,
88
)

misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.3/patches/external_static_runtime.patch renamed to misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.2/patches/external_static_runtime.patch

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,29 @@ index b77f636..1255ba7 100644
2121
"swift_version_file": attr.label(doc = "A label to the .swift_version file to use. Cannot be used concurrently with `swift_version`"),
2222
})
2323
diff --git a/swift/internal/extensions/standalone_toolchain.bzl b/swift/internal/extensions/standalone_toolchain.bzl
24-
index 801aad7..119ded9 100644
24+
index a1e09ea..1fcf56d 100644
2525
--- a/swift/internal/extensions/standalone_toolchain.bzl
2626
+++ b/swift/internal/extensions/standalone_toolchain.bzl
27-
@@ -100,6 +100,9 @@ def _standalone_toolchain_impl(repository_ctx):
27+
@@ -105,6 +105,9 @@ def _standalone_toolchain_impl(repository_ctx):
2828
"BUILD.bazel",
2929
repository_ctx.attr._build_template,
3030
substitutions = {
3131
+ "{external_static_runtime}": (
3232
+ ' external_static_runtime = "{}",'.format(repository_ctx.attr.static_runtime) if repository_ctx.attr.static_runtime else ""
3333
+ ),
34+
"{macos_sdkroot}": macos_sdkroot,
3435
"{swift_version}": repository_ctx.attr.swift_version,
3536
},
36-
)
37-
@@ -117,6 +120,9 @@ standalone_toolchain = repository_rule(
38-
"sha256": attr.string(
39-
doc = "The expected SHA-256 of the file downloaded. This must match the SHA-256 of the file downloaded.",
40-
),
41-
+ "static_runtime": attr.label(
42-
+ doc = "External static runtime package used by this execution toolchain.",
43-
+ ),
44-
"swift_version": attr.string(
45-
doc = "Version of the swift toolchain to be installed.",
46-
mandatory = True,
37+
@@ -121,6 +124,9 @@ _STANDALONE_TOOLCHAIN_ATTRS = {
38+
"sha256": attr.string(
39+
doc = "The expected SHA-256 of the file downloaded. This must match the SHA-256 of the file downloaded.",
40+
),
41+
+ "static_runtime": attr.label(
42+
+ doc = "External static runtime package used by this execution toolchain.",
43+
+ ),
44+
"swift_version": attr.string(
45+
doc = "Version of the swift toolchain to be installed.",
46+
mandatory = True,
4747
diff --git a/swift/internal/extensions/toolchain.BUILD b/swift/internal/extensions/toolchain.BUILD
4848
index 1304dc2..ee77866 100644
4949
--- a/swift/internal/extensions/toolchain.BUILD

misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.3/patches/register_downloaded_macos_toolchain.patch renamed to misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.2/patches/register_downloaded_macos_toolchain.patch

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,9 @@ diff --git a/swift/internal/extensions/toolchain.BUILD b/swift/internal/extensio
8585
+ sdkroot = "{macos_sdkroot}",
8686
static_runtime = select({
8787
diff --git a/swift/internal/extensions/standalone_toolchain.bzl b/swift/internal/extensions/standalone_toolchain.bzl
88-
index 119ded9..b7065b7 100644
8988
--- a/swift/internal/extensions/standalone_toolchain.bzl
9089
+++ b/swift/internal/extensions/standalone_toolchain.bzl
91-
@@ -95,6 +95,11 @@ def _standalone_toolchain_impl(repository_ctx):
90+
@@ -91,11 +91,17 @@ def _standalone_toolchain_impl(repository_ctx):
9291
strip_prefix = filename.removesuffix(".tar.gz"),
9392
)
9493

@@ -100,11 +99,77 @@ index 119ded9..b7065b7 100644
10099
repository_ctx.file(".swift-version", repository_ctx.attr.swift_version)
101100
repository_ctx.template(
102101
"BUILD.bazel",
103-
@@ -103,6 +108,7 @@ def _standalone_toolchain_impl(repository_ctx):
104-
"{external_static_runtime}": (
105-
' external_static_runtime = "{}",'.format(repository_ctx.attr.static_runtime) if repository_ctx.attr.static_runtime else ""
106-
),
102+
repository_ctx.attr._build_template,
103+
substitutions = {
107104
+ "{macos_sdkroot}": macos_sdkroot,
108105
"{swift_version}": repository_ctx.attr.swift_version,
109106
},
110107
)
108+
@@ -107,19 +109,29 @@ def _standalone_toolchain_impl(repository_ctx):
109+
-standalone_toolchain = repository_rule(
110+
- implementation = _standalone_toolchain_impl,
111+
- attrs = {
112+
- "_build_template": attr.label(
113+
- default = "//swift/internal/extensions:toolchain.BUILD",
114+
- ),
115+
- "platform": attr.string(
116+
- doc = "The host platform name in the swift package download URL",
117+
- mandatory = True,
118+
- ),
119+
- "sha256": attr.string(
120+
- doc = "The expected SHA-256 of the file downloaded. This must match the SHA-256 of the file downloaded.",
121+
- ),
122+
- "swift_version": attr.string(
123+
- doc = "Version of the swift toolchain to be installed.",
124+
- mandatory = True,
125+
- ),
126+
- },
127+
+_STANDALONE_TOOLCHAIN_ATTRS = {
128+
+ "_build_template": attr.label(
129+
+ default = "//swift/internal/extensions:toolchain.BUILD",
130+
+ ),
131+
+ "platform": attr.string(
132+
+ doc = "The host platform name in the swift package download URL",
133+
+ mandatory = True,
134+
+ ),
135+
+ "sha256": attr.string(
136+
+ doc = "The expected SHA-256 of the file downloaded. This must match the SHA-256 of the file downloaded.",
137+
+ ),
138+
+ "swift_version": attr.string(
139+
+ doc = "Version of the swift toolchain to be installed.",
140+
+ mandatory = True,
141+
+ ),
142+
+}
143+
+
144+
+standalone_toolchain = repository_rule(
145+
+ implementation = _standalone_toolchain_impl,
146+
+ attrs = _STANDALONE_TOOLCHAIN_ATTRS,
147+
+)
148+
+
149+
+standalone_macos_toolchain = repository_rule(
150+
+ implementation = _standalone_toolchain_impl,
151+
+ attrs = _STANDALONE_TOOLCHAIN_ATTRS,
152+
+ configure = True,
153+
+ environ = ["DEVELOPER_DIR"],
154+
+ local = True,
155+
)
156+
diff --git a/swift/extensions.bzl b/swift/extensions.bzl
157+
--- a/swift/extensions.bzl
158+
+++ b/swift/extensions.bzl
159+
@@ -17 +17,5 @@
160+
-load("//swift/internal/extensions:standalone_toolchain.bzl", "standalone_toolchain")
161+
+load(
162+
+ "//swift/internal/extensions:standalone_toolchain.bzl",
163+
+ "standalone_macos_toolchain",
164+
+ "standalone_toolchain",
165+
+)
166+
@@ -182,7 +186,8 @@ def _standalone_toolchain_impl(module_ctx):
167+
toolchain_releases = toolchain.platform_sha256.items() or swift_releases[swift_version].items()
168+
for platform, sha256 in toolchain_releases:
169+
repository_name = toolchain.name + "_{}".format(platform)
170+
- standalone_toolchain(
171+
+ toolchain_repository = standalone_macos_toolchain if platform == "xcode" else standalone_toolchain
172+
+ toolchain_repository(
173+
name = repository_name,
174+
sha256 = sha256,
175+
platform = platform,

misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.3/source.json renamed to misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.2/source.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"integrity": "sha256-Ly2lS4AlZJMiyqAxKHyCB+2pGeP5aYkQ6zv8QheeSTI=",
33
"url": "https://github.com/bazelbuild/rules_swift/releases/download/4.0.0-rc5/rules_swift.4.0.0-rc5.tar.gz",
44
"patches": {
5-
"external_static_runtime.patch": "sha256-bnyDyK3dU+vmNVKr16y0vMrtkUSYlVOkWtIBVtjqBGw=",
6-
"register_downloaded_macos_toolchain.patch": "sha256-w6/S2LEFXEFE+eLUeowKl4mQ5bLvb2SxQknXfXEglyE="
5+
"register_downloaded_macos_toolchain.patch": "sha256-249WJ4YEH2HmxlvGftl4eD53792joKJx9Dh0j5eHAgo=",
6+
"external_static_runtime.patch": "sha256-VY8UPZjCdlLjDB4taEpRfENRlkw4pGNYD2MAgNFITC4="
77
},
88
"patch_strip": 1
99
}

‎misc/bazel/registry/modules/rules_swift/metadata.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"github:bazelbuild/rules_swift"
66
],
77
"versions": [
8-
"4.0.0-rc5-codeql.3"
8+
"4.0.0-rc5-codeql.2"
99
],
1010
"yanked_versions": {}
1111
}

0 commit comments

Comments
 (0)