Skip to content

Commit e5a26bb

Browse files
committed
This PR fixes an issue where method lints (particularly unwrap_or_default) were not respecting per-item MSRV attributes. The fix ensures that when a function or module is annotated with #[clippy::msrv = "1.15"], method lints will properly respect that MSRV version.
Previously, method lints like `unwrap_or_default` would suggest using `unwrap_or_default()` even when the MSRV was set below 1.16 (when this method was stabilized). This happened because the `extract_msrv_attr!` macro was not being used in the `Methods` lint pass, so per-item MSRV attributes were not being respected. Added `extract_msrv_attr!();` to the `check_expr` method in the `Methods` lint pass implementation. This ensures that all method lints properly extract and respect per-item MSRV attributes. Added a new test case in `tests/ui/msrv_unwrap_or_default.rs` that verifies: - A function annotated with `#[clippy::msrv = "1.15"]` using `unwrap_or(vec![])` does not trigger the `unwrap_or_default` lint - The test passes with only the expected unused variable warning This change affects all method lints that check for MSRV-dependent features. The fix ensures that these lints properly respect per-item MSRV attributes, making them more accurate and useful for codebases that need to maintain compatibility with older Rust versions. Fixes #[issue number] (if there is an open issue for this) - [x] Added test case - [x] Test passes - [x] No regression in other lints - [x] Documentation is up to date
1 parent b6b3b68 commit e5a26bb

13 files changed

+455
-79
lines changed

.cursor/rules/data-flow-patterns.mdc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
description: Documents data flow patterns for static analysis and linting rules in Rust code.
3+
globs: **/src/**/*.rs,**/clippy_lints/**/*.rs,**/clippy_config/**/*.rs
4+
alwaysApply: false
5+
---
6+
7+
8+
# data-flow-patterns
9+
10+
The data flow in this linting system follows these core patterns:
11+
12+
1. Lint Registration Flow
13+
- Lints are registered through `RegistrationGroups` in lib.rs
14+
- Groups are organized by category (Correctness, Style, Complexity, etc.)
15+
- Each lint registration contains metadata like name, description, level
16+
- Registration maps to specific analysis implementations
17+
18+
2. Lint Analysis Flow
19+
- Source code is parsed into AST/MIR representations
20+
- Lint implementations analyze specific patterns in the code
21+
- Results are collected into a centralized reporting system
22+
- Early vs Late pass analysis controls when lints are applied
23+
24+
3. Configuration Processing Flow
25+
- Configuration is loaded from clippy.toml and environment
26+
- Rules are validated and normalized through conf.rs
27+
- Configuration flows to individual lint implementations
28+
- Default configurations are merged with user overrides
29+
30+
4. Data Model Validation Flow
31+
- Type-based validation rules check compatibility
32+
- Memory safety requirements are enforced through ownership analysis
33+
- Reference validity is tracked across code paths
34+
- Custom type constraints are applied based on lint requirements
35+
36+
5. Suggestion Generation Flow
37+
- Issues detected by lints generate suggestion spans
38+
- Suggestions include code replacements and rationale
39+
- Multiple suggestions may be generated per issue
40+
- Suggestions preserve original code formatting
41+
42+
Key Files:
43+
- clippy_lints/src/lib.rs - Core registration system
44+
- clippy_config/src/conf.rs - Configuration processing
45+
- clippy_lints/src/declared_lints.rs - Lint definitions
46+
- clippy_lints/src/non_copy_const.rs - Data validation
47+
48+
The system implements an extensible pipeline for static code analysis with strict separation between registration, analysis, and reporting phases.
49+
50+
$END$
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
description: Specification for analyzing and documenting lint algorithms, rules and validations in Rust static analysis tools
3+
globs: src/clippy_lints/**/*.rs,src/**/lint*.rs,src/**/rules/**/*.rs,src/**/check*.rs
4+
alwaysApply: false
5+
---
6+
7+
8+
# lint-analysis-algorithms
9+
10+
The core business logic encompasses specialized static analysis algorithms for detecting code patterns and enforcing domain rules in Rust. Key components include:
11+
12+
1. Pattern Matching Analysis
13+
- AST traversal for identifying specific code structures requiring linting
14+
- Type analysis and validation during pattern matching
15+
- Context tracking for multi-block pattern validation
16+
- Template matching against known anti-patterns
17+
18+
2. Reference Safety Rules
19+
- Borrow checker augmentation for reference analysis
20+
- Lifetime validation for borrowed values
21+
- Mutable reference access pattern checking
22+
- Reference scoping and drop timing validation
23+
24+
3. Type System Integration
25+
- Layout compatibility verification for transmutes
26+
- Generic parameter substitution tracking
27+
- Trait bound satisfaction checking
28+
- Zero-sized type special case handling
29+
30+
4. Domain Rule Enforcement
31+
- Documentation style and completeness validation
32+
- API visibility and access pattern checks
33+
- Naming convention verification
34+
- Resource safety guarantees
35+
36+
5. Suggestion Generation
37+
- Context-aware code transformation suggestions
38+
- Macro expansion preservation in suggestions
39+
- Comment and attribute retention during fixes
40+
- Edition-specific code recommendations
41+
42+
The algorithms focus on static verification of Rust-specific safety requirements and coding conventions, with extensive customization points for project-specific rules.
43+
44+
Core file paths:
45+
- clippy_lints/src/methods/*.rs
46+
- clippy_lints/src/types/*.rs
47+
- clippy_lints/src/matches/*.rs
48+
- clippy_lints/src/attrs/*.rs
49+
50+
$END$

.cursor/rules/lint-data-model.mdc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
description: Core lint metadata models and data structures used for evaluating and tracking lint rules and their relationships with Rust's type system
3+
globs: clippy_lints/src/**.rs,clippy_config/src/**.rs
4+
alwaysApply: false
5+
---
6+
7+
8+
# lint-data-model
9+
10+
## Core Lint State Model
11+
- Tracks lint metadata including name, description, level (deny/warn/allow)
12+
- Maintains contextual relationships between lints and their diagnostics
13+
- Special handling for edition-specific lint configurations
14+
- Enforces lint deprecation policies through staged transitions
15+
16+
## Lint Category Hierarchies
17+
- Implements layered categorization for lints:
18+
- Style (code formatting/convention violations)
19+
- Correctness (potential runtime errors)
20+
- Complexity (code structure issues)
21+
- Performance (inefficient patterns)
22+
- Restriction (banned patterns/code constructs)
23+
- Pedantic (best practice violations)
24+
- Nursery (new/unstable lints)
25+
26+
## Configuration Management
27+
- Handles configuration hierarchies and inheritance
28+
- Implements context-aware configuration application based on file paths
29+
- Manages per-crate and per-module lint configurations
30+
- Supports nested configuration overrides with precedence rules
31+
32+
## Diagnostic Relationship Model
33+
- Maps lints to compiler diagnostic codes
34+
- Tracks diagnostic severities and suggested fixes
35+
- Manages related diagnostic notes and explanations
36+
- Handles diagnostic suppression chains
37+
38+
Key Files:
39+
- clippy_config/src/conf.rs
40+
- clippy_lints/src/lib.rs
41+
- clippy_lints/src/declared_lints.rs
42+
43+
$END$
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
description: Specification for the type validation and safety system used in Clippy linting rules and checks.
3+
globs: clippy_lints/**/*.rs,clippy_config/**/*.rs
4+
alwaysApply: false
5+
---
6+
7+
8+
# type-validation-system
9+
10+
The type validation system comprises several key components implementing specialized safety and validation rules:
11+
12+
1. Cast Safety Validation:
13+
- Validates numeric type conversions for potential data loss
14+
- Enforces pointer safety rules during casting operations
15+
- Contains domain rules for casting between collection types
16+
- Handles specialized validation for floating point casts
17+
18+
2. Reference Type Safety:
19+
- Validates proper handling of references vs raw pointers
20+
- Implements borrowing pattern detection and safety rules
21+
- Contains custom validation for self-referential structs
22+
- Handles complex lifetime relationship validation
23+
24+
3. Collection Type Validation:
25+
- Enforces collection type safety policies
26+
- Validates element type compatibility in collections
27+
- Implements specialized slice operation safety
28+
- Contains custom validation for generic type parameters
29+
30+
4. Specialized Domain Rules:
31+
- Custom validation for mutable reference patterns
32+
- Zero-sized type handling and validation
33+
- Orphan implementation checking
34+
- Associated type relationship validation
35+
36+
Key implementations:
37+
- clippy_lints/src/casts/*.rs: Core casting validation
38+
- clippy_lints/src/types/*.rs: Type relationship checking
39+
- clippy_lints/src/transmute/*.rs: Memory transmutation safety
40+
41+
The system enforces domain-specific type safety policies through static analysis, focusing on:
42+
- Reference and pointer safety guarantees
43+
- Collection type compatibility
44+
- Memory layout validation
45+
- Generic type parameter bounds
46+
47+
Notable validations include:
48+
- Transmute operation safety
49+
- Zero-cost abstraction validation
50+
- Reference lifetime analysis
51+
- Collection element type compatibility
52+
53+
$END$

.cursorrules

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
START SPECIFICATION:
3+
---
4+
description: This overview should be applied when analyzing Clippy's linting system components, particularly focusing on custom business rules, lint implementations, and domain-specific code analysis patterns.
5+
globs: clippy_lints/src/**,clippy_config/src/**,clippy_dev/src/**
6+
alwaysApply: false
7+
---
8+
9+
10+
# main-overview
11+
12+
## Development Guidelines
13+
14+
- Only modify code directly relevant to the specific request. Avoid changing unrelated functionality.
15+
- Never replace code with placeholders like `# ... rest of the processing ...`. Always include complete code.
16+
- Break problems into smaller steps. Think through each step separately before implementing.
17+
- Always provide a complete PLAN with REASONING based on evidence from code and logs before making changes.
18+
- Explain your OBSERVATIONS clearly, then provide REASONING to identify the exact issue. Add console logs when needed to gather more information.
19+
20+
21+
Clippy implements a sophisticated static analysis system for Rust code through several key components:
22+
23+
1. Configuration Management (clippy_config/src/conf.rs)
24+
- Enforces validation rules for linting configuration
25+
- Implements custom documentation processing with code block preservation
26+
- Controls field visibility and access patterns
27+
- Validates naming conventions with configurable exceptions
28+
29+
2. Core Linting System (clippy_lints/src/lib.rs)
30+
- Organizes lints into functional categories (Correctness, Style, Complexity, etc.)
31+
- Implements two-phase lint checking:
32+
- Early pass: Syntax-level validation
33+
- Late pass: Semantic analysis
34+
- Manages centralized lint explanations and configuration metadata
35+
36+
3. Code Pattern Analysis (clippy_lints/src/matches/mod.rs)
37+
- Detects redundant match patterns and suggests simplifications
38+
- Validates mutex safety in pattern matching
39+
- Enforces pattern matching exhaustiveness
40+
- Handles reference types and binding modes
41+
42+
4. Memory Safety Validation (clippy_lints/src/transmute/)
43+
- Validates transmute operations between types
44+
- Enforces pointer safety and alignment rules
45+
- Detects invalid collection transmutes
46+
- Prevents unsafe pointer casts
47+
48+
5. Documentation Enforcement (clippy_lints/src/doc/mod.rs)
49+
- Enforces structured documentation requirements:
50+
- Safety sections for unsafe code
51+
- Error documentation for Result types
52+
- Panic documentation where relevant
53+
- Validates documentation formatting and structure
54+
55+
The system is organized around a plugin architecture where individual lints implement specific business rules for code analysis. Each lint can provide custom suggestions for code improvement while maintaining semantic correctness.
56+
57+
Key policy enforcement happens through:
58+
- Configuration validation (clippy_config)
59+
- Lint registration and management (clippy_lints/src/lib.rs)
60+
- Custom pattern analysis (clippy_lints/src/matches)
61+
- Safety rule enforcement (clippy_lints/src/transmute)
62+
63+
$END$
64+
END SPECIFICATION

.giga/specifications.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
{
3+
"fileName": "main-overview.mdc",
4+
"description": "Complete overview of Clippy's architecture, including the lint pass system, core components, and high-level workflow of lint processing"
5+
},
6+
{
7+
"fileName": "lint-data-model.mdc",
8+
"description": "Documentation of core lint data structures, including lint attributes, levels, categories, and their relationships with the Rust compiler's internal representations"
9+
},
10+
{
11+
"fileName": "lint-analysis-algorithms.mdc",
12+
"description": "Detailed documentation of key algorithms used in lint analysis, including pattern matching, AST traversal, and type checking implementations"
13+
},
14+
{
15+
"fileName": "data-flow-patterns.mdc",
16+
"description": "Documentation of how data flows through the lint system, including lint registration, application, and suggestion generation processes"
17+
},
18+
{
19+
"fileName": "type-validation-system.mdc",
20+
"description": "Detailed documentation of the type validation system, focusing on transmute checks, casting validations, and type safety enforcement algorithms"
21+
}
22+
]

clippy_lints/src/methods/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4412,7 +4412,7 @@ declare_clippy_lint! {
44124412
/// Checks for calls to `Read::bytes` on types which don't implement `BufRead`.
44134413
///
44144414
/// ### Why is this bad?
4415-
/// The default implementation calls `read` for each byte, which can be very inefficient for data thats not in memory, such as `File`.
4415+
/// The default implementation calls `read` for each byte, which can be very inefficient for data that's not in memory, such as `File`.
44164416
///
44174417
/// ### Example
44184418
/// ```no_run
@@ -4724,6 +4724,7 @@ pub fn method_call<'tcx>(recv: &'tcx Expr<'tcx>) -> Option<(Symbol, &'tcx Expr<'
47244724

47254725
impl<'tcx> LateLintPass<'tcx> for Methods {
47264726
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
4727+
extract_msrv_attr!();
47274728
if expr.span.from_expansion() {
47284729
return;
47294730
}
@@ -4741,7 +4742,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
47414742
},
47424743
ExprKind::MethodCall(method_call, receiver, args, _) => {
47434744
let method_span = method_call.ident.span;
4744-
or_fun_call::check(cx, expr, method_span, method_call.ident.name, receiver, args);
4745+
or_fun_call::check(cx, expr, method_span, method_call.ident.name, receiver, args, self.msrv);
47454746
expect_fun_call::check(
47464747
cx,
47474748
&self.format_args,

0 commit comments

Comments
 (0)