|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +use annotate_snippets::AnnotationKind; |
| 4 | +use annotate_snippets::Group; |
| 5 | +use annotate_snippets::Level; |
| 6 | +use annotate_snippets::Origin; |
| 7 | +use annotate_snippets::Patch; |
| 8 | +use annotate_snippets::Snippet; |
| 9 | +use cargo_util_schemas::manifest::TomlToolLints; |
| 10 | + |
| 11 | +use crate::CargoResult; |
| 12 | +use crate::GlobalContext; |
| 13 | +use crate::core::Package; |
| 14 | +use crate::lints::Lint; |
| 15 | +use crate::lints::LintLevel; |
| 16 | +use crate::lints::LintLevelReason; |
| 17 | +use crate::lints::RESTRICTION; |
| 18 | +use crate::lints::get_key_value_span; |
| 19 | +use crate::lints::rel_cwd_manifest_path; |
| 20 | + |
| 21 | +pub const LINT: Lint = Lint { |
| 22 | + name: "non_snake_case_features", |
| 23 | + desc: "features should have a snake-case name", |
| 24 | + primary_group: &RESTRICTION, |
| 25 | + edition_lint_opts: None, |
| 26 | + feature_gate: None, |
| 27 | + docs: Some( |
| 28 | + r#" |
| 29 | +### What it does |
| 30 | +
|
| 31 | +Detect feature names that are not snake-case. |
| 32 | +
|
| 33 | +### Why it is bad |
| 34 | +
|
| 35 | +Having multiple naming styles within a workspace can be confusing. |
| 36 | +
|
| 37 | +### Drawbacks |
| 38 | +
|
| 39 | +Users would expect that a feature tightly coupled to a dependency would match the dependency's name. |
| 40 | +
|
| 41 | +### Example |
| 42 | +
|
| 43 | +```toml |
| 44 | +[features] |
| 45 | +foo-bar = [] |
| 46 | +``` |
| 47 | +
|
| 48 | +Should be written as: |
| 49 | +
|
| 50 | +```toml |
| 51 | +[features] |
| 52 | +foo_bar = [] |
| 53 | +``` |
| 54 | +"#, |
| 55 | + ), |
| 56 | +}; |
| 57 | + |
| 58 | +pub fn non_snake_case_features( |
| 59 | + pkg: &Package, |
| 60 | + manifest_path: &Path, |
| 61 | + cargo_lints: &TomlToolLints, |
| 62 | + error_count: &mut usize, |
| 63 | + gctx: &GlobalContext, |
| 64 | +) -> CargoResult<()> { |
| 65 | + let (lint_level, reason) = LINT.level( |
| 66 | + cargo_lints, |
| 67 | + pkg.manifest().edition(), |
| 68 | + pkg.manifest().unstable_features(), |
| 69 | + ); |
| 70 | + |
| 71 | + if lint_level == LintLevel::Allow { |
| 72 | + return Ok(()); |
| 73 | + } |
| 74 | + |
| 75 | + let manifest_path = rel_cwd_manifest_path(manifest_path, gctx); |
| 76 | + |
| 77 | + lint_package(pkg, &manifest_path, lint_level, reason, error_count, gctx) |
| 78 | +} |
| 79 | + |
| 80 | +pub fn lint_package( |
| 81 | + pkg: &Package, |
| 82 | + manifest_path: &str, |
| 83 | + lint_level: LintLevel, |
| 84 | + reason: LintLevelReason, |
| 85 | + error_count: &mut usize, |
| 86 | + gctx: &GlobalContext, |
| 87 | +) -> CargoResult<()> { |
| 88 | + for original_name in pkg.summary().features().keys() { |
| 89 | + let original_name = &**original_name; |
| 90 | + let snake_case = heck::ToSnakeCase::to_snake_case(original_name); |
| 91 | + if snake_case == original_name { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + let manifest = pkg.manifest(); |
| 96 | + let document = manifest.document(); |
| 97 | + let contents = manifest.contents(); |
| 98 | + let level = lint_level.to_diagnostic_level(); |
| 99 | + let emitted_source = LINT.emitted_source(lint_level, reason); |
| 100 | + |
| 101 | + let mut primary = Group::with_title(level.primary_title(LINT.desc)); |
| 102 | + if let Some(document) = document |
| 103 | + && let Some(contents) = contents |
| 104 | + && let Some(span) = get_key_value_span(document, &["features", original_name]) |
| 105 | + { |
| 106 | + primary = primary.element( |
| 107 | + Snippet::source(contents) |
| 108 | + .path(manifest_path) |
| 109 | + .annotation(AnnotationKind::Primary.span(span.key)), |
| 110 | + ); |
| 111 | + } else if let Some(document) = document |
| 112 | + && let Some(contents) = contents |
| 113 | + && let Some(dep_span) = get_key_value_span(document, &["dependencies", original_name]) |
| 114 | + && let Some(optional_span) = |
| 115 | + get_key_value_span(document, &["dependencies", original_name, "optional"]) |
| 116 | + { |
| 117 | + primary = primary.element( |
| 118 | + Snippet::source(contents) |
| 119 | + .path(manifest_path) |
| 120 | + .annotation(AnnotationKind::Primary.span(dep_span.key)) |
| 121 | + .annotation( |
| 122 | + AnnotationKind::Context |
| 123 | + .span(optional_span.key.start..optional_span.value.end) |
| 124 | + .label("implicit feature created for optional dependencies"), |
| 125 | + ), |
| 126 | + ); |
| 127 | + } else { |
| 128 | + primary = primary.element(Origin::path(manifest_path)); |
| 129 | + } |
| 130 | + primary = primary.element(Level::NOTE.message(emitted_source)); |
| 131 | + let mut report = vec![primary]; |
| 132 | + if let Some(document) = document |
| 133 | + && let Some(contents) = contents |
| 134 | + && let Some(span) = get_key_value_span(document, &["features", original_name]) |
| 135 | + { |
| 136 | + let mut help = Group::with_title(Level::HELP.secondary_title( |
| 137 | + "to change the feature name to snake case, convert the `features` key", |
| 138 | + )); |
| 139 | + help = help.element( |
| 140 | + Snippet::source(contents) |
| 141 | + .path(manifest_path) |
| 142 | + .patch(Patch::new(span.key, snake_case.as_str())), |
| 143 | + ); |
| 144 | + report.push(help); |
| 145 | + } |
| 146 | + |
| 147 | + if lint_level.is_error() { |
| 148 | + *error_count += 1; |
| 149 | + } |
| 150 | + gctx.shell().print_report(&report, lint_level.force())?; |
| 151 | + } |
| 152 | + |
| 153 | + Ok(()) |
| 154 | +} |
0 commit comments