Skip to content

Commit 46378c2

Browse files
authored
Add clippy_error_format setting. (#3496)
This allows the error format for Clippy to be set independently of that for rustc. As this is a backwards-incompatible change, it is guarded by a feature flag `incompatible_change_clippy_error_format`. For the motivation for this change, see #3494.
1 parent 4d14035 commit 46378c2

File tree

6 files changed

+98
-5
lines changed

6 files changed

+98
-5
lines changed

BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ alias(
1313
visibility = ["//visibility:public"],
1414
)
1515

16+
alias(
17+
name = "clippy_error_format",
18+
actual = "//rust/settings:clippy_error_format",
19+
visibility = ["//visibility:public"],
20+
)
21+
1622
# This setting may be changed from the command line to generate rustc diagnostics.
1723
alias(
1824
name = "rustc_output_diagnostics",

rust/private/clippy.bzl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ load(
2121
"collect_deps",
2222
"collect_inputs",
2323
"construct_arguments",
24+
"get_error_format",
2425
)
2526
load(
2627
"//rust/private:utils.bzl",
2728
"determine_output_hash",
2829
"find_cc_toolchain",
2930
"find_toolchain",
3031
)
32+
load("//rust/settings:incompatible.bzl", "IncompatibleFlagInfo")
3133

3234
ClippyFlagsInfo = provider(
3335
doc = "Pass each value as an additional flag to clippy invocations",
@@ -138,6 +140,12 @@ def _clippy_aspect_impl(target, ctx):
138140
lint_files,
139141
)
140142

143+
use_clippy_error_format = ctx.attr._incompatible_change_clippy_error_format[IncompatibleFlagInfo].enabled
144+
error_format = get_error_format(
145+
ctx.attr,
146+
"_clippy_error_format" if use_clippy_error_format else "_error_format",
147+
)
148+
141149
args, env = construct_arguments(
142150
ctx = ctx,
143151
attr = ctx.rule.attr,
@@ -157,6 +165,7 @@ def _clippy_aspect_impl(target, ctx):
157165
build_flags_files = build_flags_files,
158166
emit = ["dep-info", "metadata"],
159167
skip_expanding_rustc_env = True,
168+
error_format = error_format,
160169
)
161170

162171
if crate_info.is_test:
@@ -233,6 +242,10 @@ rust_clippy_aspect = aspect(
233242
doc = "Value of the `capture_clippy_output` build setting",
234243
default = Label("//rust/settings:capture_clippy_output"),
235244
),
245+
"_clippy_error_format": attr.label(
246+
doc = "The desired `--error-format` flags for clippy",
247+
default = "//rust/settings:clippy_error_format",
248+
),
236249
"_clippy_flag": attr.label(
237250
doc = "Arguments to pass to clippy." +
238251
"Multiple uses are accumulated and appended after the extra_rustc_flags.",
@@ -248,12 +261,16 @@ rust_clippy_aspect = aspect(
248261
default = Label("//rust/settings:clippy.toml"),
249262
),
250263
"_error_format": attr.label(
251-
doc = "The desired `--error-format` flags for clippy",
264+
doc = "The desired `--error-format` flags for rustc",
252265
default = "//rust/settings:error_format",
253266
),
254267
"_extra_rustc_flag": attr.label(
255268
default = Label("//rust/settings:extra_rustc_flag"),
256269
),
270+
"_incompatible_change_clippy_error_format": attr.label(
271+
doc = "Whether to use the _clippy_error_format attribute",
272+
default = "//rust/settings:incompatible_change_clippy_error_format",
273+
),
257274
"_per_crate_rustc_flag": attr.label(
258275
default = Label("//rust/settings:experimental_per_crate_rustc_flag"),
259276
),

rust/private/rustc.bzl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,8 @@ def construct_arguments(
833833
use_json_output = False,
834834
build_metadata = False,
835835
force_depend_on_objects = False,
836-
skip_expanding_rustc_env = False):
836+
skip_expanding_rustc_env = False,
837+
error_format = None):
837838
"""Builds an Args object containing common rustc flags
838839
839840
Args:
@@ -865,6 +866,7 @@ def construct_arguments(
865866
build_metadata (bool): Generate CLI arguments for building *only* .rmeta files. This requires use_json_output.
866867
force_depend_on_objects (bool): Force using `.rlib` object files instead of metadata (`.rmeta`) files even if they are available.
867868
skip_expanding_rustc_env (bool): Whether to skip expanding CrateInfo.rustc_env_attr
869+
error_format (str, optional): Error format to pass to the `--error-format` command line argument. If set to None, uses the "_error_format" entry in `attr`.
868870
869871
Returns:
870872
tuple: A tuple of the following items
@@ -938,9 +940,8 @@ def construct_arguments(
938940
rustc_flags.add(crate_info.name, format = "--crate-name=%s")
939941
rustc_flags.add(crate_info.type, format = "--crate-type=%s")
940942

941-
error_format = "human"
942-
if hasattr(attr, "_error_format"):
943-
error_format = attr._error_format[ErrorFormatInfo].error_format
943+
if error_format == None:
944+
error_format = get_error_format(attr, "_error_format")
944945

945946
if use_json_output:
946947
# If --error-format was set to json, we just pass the output through
@@ -2254,6 +2255,11 @@ error_format = rule(
22542255
build_setting = config.string(flag = True),
22552256
)
22562257

2258+
def get_error_format(attr, attr_name):
2259+
if hasattr(attr, attr_name):
2260+
return getattr(attr, attr_name)[ErrorFormatInfo].error_format
2261+
return "human"
2262+
22572263
def _rustc_output_diagnostics_impl(ctx):
22582264
"""Implementation of the `rustc_output_diagnostics` rule
22592265

rust/settings/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
22
load(
33
":settings.bzl",
44
"capture_clippy_output",
5+
"clippy_error_format",
56
"clippy_flag",
67
"clippy_flags",
78
"clippy_toml",
@@ -17,6 +18,7 @@ load(
1718
"extra_exec_rustc_flags",
1819
"extra_rustc_flag",
1920
"extra_rustc_flags",
21+
"incompatible_change_clippy_error_format",
2022
"incompatible_change_rust_test_compilation_output_directory",
2123
"incompatible_do_not_include_data_in_compile_data",
2224
"lto",
@@ -61,6 +63,8 @@ codegen_units()
6163

6264
error_format()
6365

66+
clippy_error_format()
67+
6468
experimental_link_std_dylib()
6569

6670
experimental_per_crate_rustc_flag()
@@ -81,6 +85,8 @@ extra_rustc_flag()
8185

8286
extra_rustc_flags()
8387

88+
incompatible_change_clippy_error_format()
89+
8490
incompatible_change_rust_test_compilation_output_directory()
8591

8692
incompatible_do_not_include_data_in_compile_data()

rust/settings/settings.bzl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,28 @@ def error_format():
237237
build_setting_default = "human",
238238
)
239239

240+
# buildifier: disable=unnamed-macro
241+
def clippy_error_format():
242+
"""This setting may be changed from the command line to generate machine readable errors.
243+
"""
244+
_error_format(
245+
name = "clippy_error_format",
246+
build_setting_default = "human",
247+
)
248+
249+
# buildifier: disable=unnamed-macro
250+
def incompatible_change_clippy_error_format():
251+
"""A flag to enable the `clippy_error_format` setting.
252+
253+
If this flag is true, Clippy uses the format set in `clippy_error_format` to
254+
format its diagnostics; otherwise, it uses the format set in `error_format`.
255+
"""
256+
incompatible_flag(
257+
name = "incompatible_change_clippy_error_format",
258+
build_setting_default = False,
259+
issue = "https://github.com/bazelbuild/rules_rust/issues/3494",
260+
)
261+
240262
# buildifier: disable=unnamed-macro
241263
def rustc_output_diagnostics():
242264
"""This setting may be changed from the command line to generate rustc diagnostics.

test/unit/clippy/clippy_test.bzl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,30 @@ clippy_aspect_with_explicit_flags_test = make_clippy_aspect_unittest(
9999
},
100100
)
101101

102+
clippy_aspect_without_clippy_error_format_test = make_clippy_aspect_unittest(
103+
lambda ctx: _clippy_aspect_action_has_flag_impl(
104+
ctx,
105+
["--error-format=short"],
106+
),
107+
config_settings = {
108+
str(Label("//rust/settings:error_format")): "short",
109+
str(Label("//rust/settings:clippy_error_format")): "json",
110+
str(Label("//rust/settings:incompatible_change_clippy_error_format")): False,
111+
},
112+
)
113+
114+
clippy_aspect_with_clippy_error_format_test = make_clippy_aspect_unittest(
115+
lambda ctx: _clippy_aspect_action_has_flag_impl(
116+
ctx,
117+
["--error-format=json"],
118+
),
119+
config_settings = {
120+
str(Label("//rust/settings:error_format")): "short",
121+
str(Label("//rust/settings:clippy_error_format")): "json",
122+
str(Label("//rust/settings:incompatible_change_clippy_error_format")): True,
123+
},
124+
)
125+
102126
def clippy_test_suite(name):
103127
"""Entry-point macro called from the BUILD file.
104128
@@ -118,6 +142,7 @@ def clippy_test_suite(name):
118142
name = "test_clippy_aspect_action_has_warnings_flag_test",
119143
target_under_test = Label("//test/clippy:ok_test"),
120144
)
145+
121146
clippy_aspect_with_explicit_flags_test(
122147
name = "binary_clippy_aspect_with_explicit_flags_test",
123148
target_under_test = Label("//test/clippy:ok_binary"),
@@ -131,6 +156,15 @@ def clippy_test_suite(name):
131156
target_under_test = Label("//test/clippy:ok_test"),
132157
)
133158

159+
clippy_aspect_without_clippy_error_format_test(
160+
name = "clippy_aspect_without_clippy_error_format_test",
161+
target_under_test = Label("//test/clippy:ok_library"),
162+
)
163+
clippy_aspect_with_clippy_error_format_test(
164+
name = "clippy_aspect_with_clippy_error_format_test",
165+
target_under_test = Label("//test/clippy:ok_library"),
166+
)
167+
134168
native.test_suite(
135169
name = name,
136170
tests = [
@@ -140,5 +174,7 @@ def clippy_test_suite(name):
140174
":binary_clippy_aspect_with_explicit_flags_test",
141175
":library_clippy_aspect_with_explicit_flags_test",
142176
":test_clippy_aspect_with_explicit_flags_test",
177+
":clippy_aspect_without_clippy_error_format_test",
178+
":clippy_aspect_with_clippy_error_format_test",
143179
],
144180
)

0 commit comments

Comments
 (0)