|
| 1 | +//! Copied from rls/src/config.rs |
| 2 | +
|
| 3 | +use std::str::FromStr; |
| 4 | + |
| 5 | +#[derive(Debug, Clone, Copy, PartialEq)] |
| 6 | +pub enum ClippyPreference { |
| 7 | + /// Disable clippy. |
| 8 | + Off, |
| 9 | + /// Enable clippy, but `allow` clippy lints (i.e., require `warn` override). |
| 10 | + OptIn, |
| 11 | + /// Enable clippy. |
| 12 | + On, |
| 13 | +} |
| 14 | + |
| 15 | +pub fn preference() -> Option<ClippyPreference> { |
| 16 | + std::env::var("RLS_CLIPPY_PREFERENCE").ok().and_then(|pref| FromStr::from_str(&pref).ok()) |
| 17 | +} |
| 18 | + |
| 19 | +/// Permissive deserialization for `ClippyPreference` |
| 20 | +/// "opt-in", "Optin" -> `ClippyPreference::OptIn` |
| 21 | +impl FromStr for ClippyPreference { |
| 22 | + type Err = (); |
| 23 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 24 | + match s.to_lowercase().as_str() { |
| 25 | + "off" => Ok(ClippyPreference::Off), |
| 26 | + "optin" | "opt-in" => Ok(ClippyPreference::OptIn), |
| 27 | + "on" => Ok(ClippyPreference::On), |
| 28 | + _ => Err(()), |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +pub fn adjust_args(args: Vec<String>, preference: ClippyPreference) -> Vec<String> { |
| 34 | + if preference != ClippyPreference::Off { |
| 35 | + // Allow feature gating in the same way as `cargo clippy` |
| 36 | + let mut clippy_args = vec!["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]; |
| 37 | + |
| 38 | + if preference == ClippyPreference::OptIn { |
| 39 | + // `OptIn`: Require explicit `#![warn(clippy::all)]` annotation in each workspace crate |
| 40 | + clippy_args.push("-A".to_owned()); |
| 41 | + clippy_args.push("clippy::all".to_owned()); |
| 42 | + } |
| 43 | + |
| 44 | + args.iter().map(ToOwned::to_owned).chain(clippy_args).collect() |
| 45 | + } else { |
| 46 | + args.to_owned() |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +#[cfg(feature = "clippy")] |
| 51 | +pub fn after_parse_callback(compiler: &rustc_interface::interface::Compiler) { |
| 52 | + use rustc_plugin::registry::Registry; |
| 53 | + |
| 54 | + let sess = compiler.session(); |
| 55 | + let mut registry = Registry::new( |
| 56 | + sess, |
| 57 | + compiler |
| 58 | + .parse() |
| 59 | + .expect( |
| 60 | + "at this compilation stage \ |
| 61 | + the crate must be parsed", |
| 62 | + ) |
| 63 | + .peek() |
| 64 | + .span, |
| 65 | + ); |
| 66 | + registry.args_hidden = Some(Vec::new()); |
| 67 | + |
| 68 | + let conf = clippy_lints::read_conf(®istry); |
| 69 | + clippy_lints::register_plugins(&mut registry, &conf); |
| 70 | + |
| 71 | + let Registry { |
| 72 | + early_lint_passes, late_lint_passes, lint_groups, llvm_passes, attributes, .. |
| 73 | + } = registry; |
| 74 | + let mut ls = sess.lint_store.borrow_mut(); |
| 75 | + for pass in early_lint_passes { |
| 76 | + ls.register_early_pass(Some(sess), true, false, pass); |
| 77 | + } |
| 78 | + for pass in late_lint_passes { |
| 79 | + ls.register_late_pass(Some(sess), true, false, false, pass); |
| 80 | + } |
| 81 | + |
| 82 | + for (name, (to, deprecated_name)) in lint_groups { |
| 83 | + ls.register_group(Some(sess), true, name, deprecated_name, to); |
| 84 | + } |
| 85 | + clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf); |
| 86 | + clippy_lints::register_renamed(&mut ls); |
| 87 | + |
| 88 | + sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes); |
| 89 | + sess.plugin_attributes.borrow_mut().extend(attributes); |
| 90 | +} |
0 commit comments