Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions crates/tombi-linter/tests/integration/cargo_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,45 @@ test_lint! {
) -> Ok(_)
}

test_lint! {
#[test]
fn test_package_authors_deprecated_disabled_does_not_report_unused_noqa(
r#"
[package]
name = "x"
version = "0.1.0"
# tombi: lint.rules.deprecated.disabled = true
authors = ["x"]
"#,
SchemaPath(cargo_schema_path()),
) -> Ok(_)
}

test_lint! {
#[test]
fn test_package_authors_deprecated_disabled_trailing_does_not_report_unused_noqa(
r#"
[package]
name = "x"
version = "0.1.0"
authors = ["x"] # tombi: lint.rules.deprecated.disabled = true
"#,
SchemaPath(cargo_schema_path()),
) -> Ok(_)
}

test_lint! {
#[test]
fn test_workspace_package_authors_deprecated_disabled_does_not_report_unused_noqa(
r#"
[workspace.package]
# tombi: lint.rules.deprecated.disabled = true
authors = ["x"]
"#,
SchemaPath(cargo_schema_path()),
) -> Ok(_)
}

test_lint! {
#[test]
fn test_package_name_wrong_type_with_wrong_comment_directive_disabled_eq_true(
Expand Down Expand Up @@ -735,3 +774,65 @@ test_lint! {
Config(deprecated_schema_config(None)),
) -> Ok(_)
}

test_lint! {
#[test]
fn test_deprecated_any_of_branch_disabled_does_not_report_unused_noqa(
"any_of_value = 1 # tombi: lint.rules.deprecated.disabled = true\n",
Config(deprecated_schema_config(None)),
) -> Ok(_)
}

test_lint! {
#[test]
fn test_deprecated_any_of_non_deprecated_branch_disabled_reports_unused_noqa(
"any_of_value = \"x\" # tombi: lint.rules.deprecated.disabled = true\n",
Config(deprecated_schema_config(None)),
) -> Diagnostics([{
code: "unused-noqa",
level: tombi_diagnostic::Level::WARNING,
}])
}

test_lint! {
#[test]
fn test_deprecated_one_of_branch_disabled_does_not_report_unused_noqa(
"one_of_value = 1 # tombi: lint.rules.deprecated.disabled = true\n",
Config(deprecated_schema_config(None)),
) -> Ok(_)
}

test_lint! {
#[test]
fn test_deprecated_all_of_branch_disabled_does_not_report_unused_noqa(
"all_of_value = 1 # tombi: lint.rules.deprecated.disabled = true\n",
Config(deprecated_schema_config(None)),
) -> Ok(_)
}

test_lint! {
#[test]
fn test_deprecated_any_of_overlapping_branches_disabled_does_not_report_unused_noqa(
"any_of_overlapping_value = 1 # tombi: lint.rules.deprecated.disabled = true\n",
Config(deprecated_schema_config(None)),
) -> Ok(_)
}

test_lint! {
#[test]
fn test_deprecated_all_of_overlapping_branches_disabled_does_not_report_unused_noqa(
"all_of_overlapping_value = 1 # tombi: lint.rules.deprecated.disabled = true\n",
Config(deprecated_schema_config(None)),
) -> Ok(_)
}

test_lint! {
#[test]
fn test_deprecated_all_of_non_deprecated_branches_disabled_reports_unused_noqa_once(
"all_of_non_deprecated_value = 1 # tombi: lint.rules.deprecated.disabled = true\n",
Config(deprecated_schema_config(None)),
) -> Diagnostics([{
code: "unused-noqa",
level: tombi_diagnostic::Level::WARNING,
}])
}
39 changes: 39 additions & 0 deletions crates/tombi-validator/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,45 @@ fn handle_unused_noqa<'a>(
}
}

/// Merge diagnostics from combinator branches.
///
/// A comment directive is unused only if every branch reports it as unused,
/// so `unused-noqa` diagnostics are kept only when all branches agree.
pub(crate) fn merge_branch_diagnostics(
branch_diagnostics: Vec<Vec<tombi_diagnostic::Diagnostic>>,
) -> Vec<tombi_diagnostic::Diagnostic> {
fn is_same_diagnostic(
a: &tombi_diagnostic::Diagnostic,
b: &tombi_diagnostic::Diagnostic,
) -> bool {
a.code() == b.code() && a.message() == b.message() && a.range() == b.range()
}

if !branch_diagnostics
.iter()
.flatten()
.any(|diagnostic| diagnostic.code() == "unused-noqa")
{
return branch_diagnostics.into_iter().flatten().collect();
}

let mut merged: Vec<tombi_diagnostic::Diagnostic> = Vec::new();
for diagnostic in branch_diagnostics.iter().flatten() {
let keep = diagnostic.code() != "unused-noqa"
|| (branch_diagnostics.iter().all(|others| {
others
.iter()
.any(|other| is_same_diagnostic(diagnostic, other))
}) && !merged
.iter()
.any(|other| is_same_diagnostic(diagnostic, other)));
if keep {
merged.push(diagnostic.clone());
}
}
merged
}

#[allow(clippy::result_large_err)]
pub(crate) fn with_lint_diagnostics(
result: Result<crate::Valid, crate::Invalid>,
Expand Down
14 changes: 10 additions & 4 deletions crates/tombi-validator/src/validate/all_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use tombi_future::{BoxFuture, Boxable};
use tombi_schema_store::CurrentSchema;

use crate::validate::{
handle_deprecated, if_then_else::validate_if_then_else, not_schema::validate_not,
handle_deprecated, if_then_else::validate_if_then_else, merge_branch_diagnostics,
not_schema::validate_not,
};

use super::Validate;
Expand Down Expand Up @@ -68,25 +69,30 @@ where
}
}
} else {
let mut branch_diagnostics = Vec::with_capacity(resolved_schemas.len());
for resolved_schema in &resolved_schemas {
match value
.validate(accessors, Some(resolved_schema), schema_context)
.await
{
Ok(result) => evaluated_locations.merge_from(result),
Ok(result) => {
evaluated_locations.merge_from(result);
branch_diagnostics.push(Vec::new());
}
Err(error) => {
if !error.assertion_failed {
evaluated_locations.merge_from(error.local_evaluated_locations.clone());
}
assertion_failed |= error.assertion_failed;
total_diagnostics.extend(error.diagnostics);
branch_diagnostics.push(error.diagnostics);
match_evidence.merge_from(*error.match_evidence);
}
}
}
total_diagnostics.extend(merge_branch_diagnostics(branch_diagnostics));
}

if total_diagnostics.is_empty() {
if total_diagnostics.is_empty() && all_of_schema.deprecation.is_some() {
Comment thread
ya7010 marked this conversation as resolved.
handle_deprecated(
&mut total_diagnostics,
all_of_schema.deprecation.as_ref(),
Expand Down
31 changes: 19 additions & 12 deletions crates/tombi-validator/src/validate/any_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tombi_schema_store::CurrentSchema;

use super::Validate;
use crate::validate::{if_then_else::validate_if_then_else, not_schema::validate_not};
use crate::validate::{validate_deprecated, validate_resolved_schema};
use crate::validate::{merge_branch_diagnostics, validate_deprecated, validate_resolved_schema};

pub fn validate_any_of<'a: 'b, 'b, T>(
value: &'a T,
Expand Down Expand Up @@ -100,7 +100,7 @@ where

let mut total_error = crate::Invalid::new();
let mut matched = false;
let mut matched_diagnostics = Vec::new();
let mut matched_branch_diagnostics = Vec::new();
let mut matched_evaluated_locations = base_evaluated_locations;

for resolved_schema in &resolved_schemas {
Expand All @@ -120,6 +120,7 @@ where
Ok(result) => {
matched = true;
matched_evaluated_locations.merge_from(result);
matched_branch_diagnostics.push(Vec::new());
}
Err(error) => {
if error.assertion_failed {
Expand All @@ -128,22 +129,28 @@ where
matched = true;
matched_evaluated_locations
.merge_from(error.local_evaluated_locations.clone());
matched_diagnostics.extend(error.diagnostics);
matched_branch_diagnostics.push(error.diagnostics);
}
}
}
}

let mut matched_diagnostics = merge_branch_diagnostics(matched_branch_diagnostics);

if matched {
if let Err(error) = validate_deprecated(
any_of_schema.deprecation.as_ref(),
accessors,
value,
Some(current_schema),
schema_context,
comment_directives,
common_rules,
) {
// Only report `unused-noqa` for the deprecated rule at the leaf schema;
// a matched branch may have consumed the directive.
if any_of_schema.deprecation.is_some()
Comment thread
ya7010 marked this conversation as resolved.
&& let Err(error) = validate_deprecated(
any_of_schema.deprecation.as_ref(),
accessors,
value,
Some(current_schema),
schema_context,
comment_directives,
common_rules,
)
{
matched_diagnostics.extend(error.diagnostics);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/tombi-validator/src/validate/one_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ where
a
});

if error.diagnostics.is_empty() {
if error.diagnostics.is_empty() && one_of_schema.deprecation.is_some() {
handle_deprecated(
&mut error.diagnostics,
one_of_schema.deprecation.as_ref(),
Expand Down
30 changes: 30 additions & 0 deletions schemas/deprecated-test.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,36 @@
"message_only": {
"type": "integer",
"deprecationMessage": "Use `value` instead"
},
"any_of_value": {
"anyOf": [
{ "type": "integer", "deprecated": true },
{ "type": "string" }
]
},
"one_of_value": {
"oneOf": [
{ "type": "integer", "deprecated": true },
{ "type": "string" }
]
},
"all_of_value": {
"allOf": [{ "type": "integer", "deprecated": true }]
},
"any_of_overlapping_value": {
"anyOf": [
{ "type": "integer", "deprecated": true },
{ "type": "integer" }
]
},
"all_of_overlapping_value": {
"allOf": [
{ "type": "integer", "deprecated": true },
{ "type": "integer" }
]
},
"all_of_non_deprecated_value": {
"allOf": [{ "type": "integer" }, { "type": "integer" }]
}
},
"additionalProperties": false
Expand Down
Loading