|
| 1 | +#!/bin/bash |
| 2 | +# Doc Comment Accuracy Check for Fortress Rollback |
| 3 | +# |
| 4 | +# Checks that doc comments mentioning "downcast" are backed by actual |
| 5 | +# downcasting infrastructure in the same file. This prevents misleading |
| 6 | +# documentation that references capabilities the code doesn't support. |
| 7 | +# |
| 8 | +# Usage: ./scripts/ci/check-doc-claims.sh [options] |
| 9 | +# ./scripts/ci/check-doc-claims.sh # Check all Rust files |
| 10 | +# ./scripts/ci/check-doc-claims.sh --verbose # Show all files checked |
| 11 | +# ./scripts/ci/check-doc-claims.sh --help # Show help |
| 12 | +# |
| 13 | +# Exit codes: |
| 14 | +# 0 - No issues found |
| 15 | +# 1 - Misleading doc comments detected |
| 16 | + |
| 17 | +set -euo pipefail |
| 18 | + |
| 19 | +# Configuration |
| 20 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 21 | +PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" |
| 22 | + |
| 23 | +# Colors |
| 24 | +RED='\033[0;31m' |
| 25 | +GREEN='\033[0;32m' |
| 26 | +YELLOW='\033[1;33m' |
| 27 | +BLUE='\033[0;34m' |
| 28 | +NC='\033[0m' # No Color |
| 29 | + |
| 30 | +# Options |
| 31 | +VERBOSE=false |
| 32 | + |
| 33 | +print_usage() { |
| 34 | + echo "Usage: $0 [options]" |
| 35 | + echo "" |
| 36 | + echo "Options:" |
| 37 | + echo " --verbose Show all files checked" |
| 38 | + echo " --help Show this help message" |
| 39 | + echo "" |
| 40 | + echo "Checks doc comments for claims about downcasting that aren't" |
| 41 | + echo "backed by actual downcasting infrastructure in the same file." |
| 42 | +} |
| 43 | + |
| 44 | +main() { |
| 45 | + # Parse arguments |
| 46 | + while [[ $# -gt 0 ]]; do |
| 47 | + case "$1" in |
| 48 | + --verbose) |
| 49 | + VERBOSE=true |
| 50 | + shift |
| 51 | + ;; |
| 52 | + --help) |
| 53 | + print_usage |
| 54 | + exit 0 |
| 55 | + ;; |
| 56 | + *) |
| 57 | + echo "Unknown argument: $1" |
| 58 | + print_usage |
| 59 | + exit 1 |
| 60 | + ;; |
| 61 | + esac |
| 62 | + done |
| 63 | + |
| 64 | + echo "==========================================" |
| 65 | + echo " Doc Comment Accuracy Check" |
| 66 | + echo "==========================================" |
| 67 | + echo "" |
| 68 | + |
| 69 | + # Patterns that indicate actual downcasting infrastructure |
| 70 | + # If a file mentions downcasting in docs, it should contain at least one of these |
| 71 | + local downcast_infra_patterns='(as_any|downcast_ref|downcast_mut|dyn Any|: Any|impl Any|Any \+|Any\+|\.downcast\b)' |
| 72 | + |
| 73 | + local issues=0 |
| 74 | + local files_with_claims=0 |
| 75 | + |
| 76 | + # Find all Rust source files (excluding target directories) |
| 77 | + while IFS= read -r file; do |
| 78 | + [[ -z "$file" ]] && continue |
| 79 | + |
| 80 | + local rel_path="${file#"$PROJECT_ROOT/"}" |
| 81 | + |
| 82 | + # Find doc comment lines mentioning "downcast" (case-insensitive) |
| 83 | + local doc_matches |
| 84 | + doc_matches=$(grep -niE '^\s*///.*downcast|^\s*//!.*downcast' "$file" 2>/dev/null || true) |
| 85 | + |
| 86 | + if [[ -z "$doc_matches" ]]; then |
| 87 | + if [[ "$VERBOSE" == "true" ]]; then |
| 88 | + echo -e " ${GREEN}OK${NC}: $rel_path (no downcast doc claims)" |
| 89 | + fi |
| 90 | + continue |
| 91 | + fi |
| 92 | + |
| 93 | + files_with_claims=$((files_with_claims + 1)) |
| 94 | + |
| 95 | + if [[ "$VERBOSE" == "true" ]]; then |
| 96 | + echo -e " ${YELLOW}Checking${NC}: $rel_path (has downcast doc claims)" |
| 97 | + fi |
| 98 | + |
| 99 | + # Check if the file has actual downcasting infrastructure |
| 100 | + local has_infra |
| 101 | + has_infra=0 |
| 102 | + has_infra=$(grep -cE "$downcast_infra_patterns" "$file" 2>/dev/null) || true |
| 103 | + |
| 104 | + if [[ "$has_infra" -eq 0 ]]; then |
| 105 | + issues=$((issues + 1)) |
| 106 | + echo "" |
| 107 | + echo -e "${RED}ERROR${NC}: $rel_path" |
| 108 | + echo -e " Doc comments mention \"downcast\" but no downcasting infrastructure found." |
| 109 | + echo -e " ${YELLOW}Doc comment(s):${NC}" |
| 110 | + while IFS= read -r match_line; do |
| 111 | + echo -e " $match_line" |
| 112 | + done <<< "$doc_matches" |
| 113 | + echo -e " ${BLUE}Expected one of:${NC} as_any, downcast_ref, downcast_mut, dyn Any, : Any" |
| 114 | + echo -e " ${BLUE}Fix:${NC} Either add downcasting support or update the doc comment" |
| 115 | + echo -e " to accurately describe the actual pattern used." |
| 116 | + else |
| 117 | + if [[ "$VERBOSE" == "true" ]]; then |
| 118 | + echo -e " ${GREEN}OK${NC}: downcasting infrastructure found ($has_infra occurrence(s))" |
| 119 | + fi |
| 120 | + fi |
| 121 | + |
| 122 | + done < <(find "$PROJECT_ROOT/src" "$PROJECT_ROOT/tests" "$PROJECT_ROOT/examples" "$PROJECT_ROOT/benches" \ |
| 123 | + -name '*.rs' -print 2>/dev/null \ |
| 124 | + | sort) |
| 125 | + |
| 126 | + echo "" |
| 127 | + |
| 128 | + if [[ "$issues" -eq 0 ]]; then |
| 129 | + echo -e "${GREEN}SUCCESS: No misleading downcast doc claims found.${NC}" |
| 130 | + if [[ "$files_with_claims" -gt 0 ]]; then |
| 131 | + echo -e " ($files_with_claims file(s) with downcast references verified)" |
| 132 | + fi |
| 133 | + exit 0 |
| 134 | + fi |
| 135 | + |
| 136 | + echo -e "${RED}FAILED: $issues file(s) have misleading downcast doc claims.${NC}" |
| 137 | + echo "" |
| 138 | + echo "Doc comments should accurately describe the code's capabilities." |
| 139 | + echo "If downcasting isn't supported, describe the actual pattern instead." |
| 140 | + exit 1 |
| 141 | +} |
| 142 | + |
| 143 | +main "$@" |
0 commit comments