Skip to content

feat(linter): add const_and_enum_conflict rule#2290

Open
AcEKaycgR wants to merge 1 commit intosourcemeta:mainfrom
AcEKaycgR:feat/linter-const-and-enum-conflict
Open

feat(linter): add const_and_enum_conflict rule#2290
AcEKaycgR wants to merge 1 commit intosourcemeta:mainfrom
AcEKaycgR:feat/linter-const-and-enum-conflict

Conversation

@AcEKaycgR
Copy link

Summary

This PR introduces a new lint rule: const_and_enum_conflict.

The rule detects schemas that declare both const and enum keywords simultaneously.

{
  "const": 1,
  "enum": [1, 2, 3]
}

Using both const and enum together is redundant and contradictory. const is equivalent to a single-value enum, so having both is always an authoring mistake and should be reported to the user.

The rule emits a diagnostic when:

  • const is defined
  • enum is defined
  • both exist on the same schema object

The diagnostic points to both the const and enum keyword locations.

This rule is non auto-fixable because the correct resolution depends on the schema author's intent (whether to keep const or enum).

Implementation

The rule follows the existing alterschema linter architecture:

  • implemented as a header-only rule in src/extension/alterschema/linter/const_and_enum_conflict.h
  • registered in alterschema.cc
  • added to the SOURCES list in the alterschema CMake configuration

The rule checks:

  • schema node is an object
  • const keyword is defined
  • enum keyword is defined

Note: The rule only applies from Draft 6 onward, as const was introduced in Draft 6.

If these conditions are met the rule returns:

APPLIES_TO_KEYWORDS("const", "enum")

Tests

Tests were added across supported dialects using the existing lint testing utilities.

The following scenarios are covered:

  • Rule fires when both const and enum are present
  • Rule does not fire when only const is present
  • Rule does not fire when only enum is present
  • Rule fires inside nested subschemas (e.g. inside properties)

All tests follow the patterns used by existing lint rule tests.

Related Work

This rule addresses one of the pending lint rules listed in:

Refs: #1975


@jviotti Could you please review this pull request when you have a moment?

If the approach looks good, I'd be happy to continue implementing additional linting rules listed in #1975.

cc @Karan-Palan

Note: PR description and test structure follow the same format as 2287 for consistency.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 7 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="test/alterschema/alterschema_lint_2019_09_test.cc">

<violation number="1" location="test/alterschema/alterschema_lint_2019_09_test.cc:4512">
P2: New const_and_enum_conflict tests do not assert both diagnostic keyword locations, so regressions in secondary-location reporting would go undetected.</violation>
</file>

<file name="test/alterschema/alterschema_lint_2020_12_test.cc">

<violation number="1" location="test/alterschema/alterschema_lint_2020_12_test.cc:9682">
P2: New `const_and_enum_conflict` tests do not assert the diagnostic’s dual keyword locations, so regressions in secondary-location reporting would go undetected.</violation>
</file>

Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Add one-off context when rerunning by tagging @cubic-dev-ai with guidance or docs links (including llms.txt)
  • Ask questions if you need clarification on any suggestion

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.


EXPECT_FALSE(result.first);
EXPECT_EQ(traces.size(), 1);
EXPECT_LINT_TRACE(traces, 0, "", "const_and_enum_conflict",
Copy link

@cubic-dev-ai cubic-dev-ai bot Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: New const_and_enum_conflict tests do not assert the diagnostic’s dual keyword locations, so regressions in secondary-location reporting would go undetected.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At test/alterschema/alterschema_lint_2020_12_test.cc, line 9682:

<comment>New `const_and_enum_conflict` tests do not assert the diagnostic’s dual keyword locations, so regressions in secondary-location reporting would go undetected.</comment>

<file context>
@@ -9664,3 +9664,77 @@ TEST(AlterSchema_lint_2020_12, object_oneof_required_not_required_6) {
+
+  EXPECT_FALSE(result.first);
+  EXPECT_EQ(traces.size(), 1);
+  EXPECT_LINT_TRACE(traces, 0, "", "const_and_enum_conflict",
+                    "The `const` and `enum` keywords are mutually exclusive; "
+                    "use one or the other, not both",
</file context>
Fix with Cubic

@AcEKaycgR AcEKaycgR force-pushed the feat/linter-const-and-enum-conflict branch 2 times, most recently from 074cd14 to be2a9a0 Compare March 10, 2026 07:21
Signed-off-by: AcE <kintan0108@gmail.com>
@AcEKaycgR AcEKaycgR force-pushed the feat/linter-const-and-enum-conflict branch from be2a9a0 to d8c5e5d Compare March 10, 2026 07:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant