Skip to content

Commit

Permalink
lint: add bad opt access internal lint
Browse files Browse the repository at this point in the history
Some command-line options accessible through `sess.opts` are best
accessed through wrapper functions on `Session`, `TyCtxt` or otherwise,
rather than through field access on the option struct in the `Session`.

Adds a new lint which triggers on those options that should be accessed
through a wrapper function so that this is prohibited. Options are
annotated with a new attribute `rustc_lint_opt_deny_field_access` which
can specify the error message (i.e. "use this other function instead")
to be emitted.

A simpler alternative would be to simply rename the options in the
option type so that it is clear they should not be used, however this
doesn't prevent uses, just discourages them. Another alternative would
be to make the option fields private, and adding accessor functions on
the option types, however the wrapper functions sometimes rely on
additional state from `Session` or `TyCtxt` which wouldn't be available
in an function on the option type, so the accessor would simply make the
field available and its use would be discouraged too.

Signed-off-by: David Wood <david.wood@huawei.com>
  • Loading branch information
davidtwco committed Jul 27, 2022
1 parent 6654aab commit 2572591
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ struct ClippyCallbacks {
}

impl rustc_driver::Callbacks for ClippyCallbacks {
// JUSTIFICATION: necessary in clippy driver to set `mir_opt_level`
#[cfg_attr(not(bootstrap), allow(rustc::bad_opt_access))]
fn config(&mut self, config: &mut interface::Config) {
let previous = config.register_lints.take();
let clippy_args_var = self.clippy_args_var.take();
Expand Down

0 comments on commit 2572591

Please sign in to comment.