1- [ // ] : # " Include this file in the repository to provide instructions to GitHub Copilot AUtofix. For more information, see https://docs.github.com/copilot/copilot-for-business/copilot-instructions. "
2- # GitHub Copilot instructions
1+ [ // ] : # " Include this file in the repository to provide instructions to GitHub Copilot Autofix. For more information, see https://docs.github.com/copilot/copilot-for-business/copilot-instructions. "
32
4- This file contains repository-wide guidance for GitHub Copilot. Each top-level
5- section below configures Copilot for a specific use case in this repository.
6- Add further top-level sections as needed (general coding conventions, review
7- guidance, etc.).
83
9- ## Autofix for CodeQL Coding Standards
4+ # Agentic autofix instructions for CodeQL Coding Standards
105
11- This section configures GitHub Copilot ** Autofix** when it generates pull requests to remediate alerts produced by the
12- [ CodeQL Coding Standards] ( https://github.com/github/codeql-coding-standards/ )
13- project. It applies to alerts for any of the supported standards (MISRA C,
14- MISRA C++, AUTOSAR C++, CERT C, CERT C++).
6+ This document configures ** Agentic Autofix** when generating pull requests to
7+ remediate alerts produced by [ CodeQL Coding Standards] ( https://github.com/github/codeql-coding-standards/ ) .
8+ It applies to alerts for any of the supported standards (MISRA C, MISRA C++ ,
9+ AUTOSAR C++, CERT C, CERT C++).
1510
16- ### 1. Reference material — where to learn each rule
11+ ## 1. Reference material — where to learn each rule
1712
18- Before proposing a fix, consult the rule’s authoritative implementation as well as the corresponding compliant and non-compliant code patterns available as test cases in the CodeQL Coding Standards [ ` github/codeql-coding-standards ` ] ( https://github.com/github/codeql-coding-standards/ )
19- repository. That repository is the single source of truth for what the query detects and what compliant code looks like.
13+ Before proposing a fix, consult the rule’s authoritative implementation as well
14+ as the corresponding compliant and non-compliant code patterns available as
15+ test cases in the [ ` github/codeql-coding-standards ` repository] ( https://github.com/github/codeql-coding-standards/ ) .
16+ That repository is the single source of truth for what the query
17+ detects and what compliant code looks like.
2018
2119Project layout (per language / standard):
2220
@@ -41,33 +39,48 @@ The full list of supported rules per standard is published as
4139` supported_rules_list_<version>.csv ` / ` .md ` in each
4240[ release] ( https://github.com/github/codeql-coding-standards/releases ) .
4341
44- ### 2. Fix discipline — keep changes minimal and standards-compliant
42+ ## 2. Fix discipline — keep changes minimal and standards-compliant
4543
4644- ** Minimum diff.** Modify the smallest possible amount of code that
47- eliminates the assigned alerts . Do not refactor surrounding code, rename symbols,
45+ eliminates the alert . Do not refactor surrounding code, rename symbols,
4846 reformat unrelated lines, or change public APIs unless strictly required to
4947 satisfy the rule.
5048- ** No drive-by changes.** Do not add features, fix unrelated warnings, change
5149 build flags, update dependencies, or “improve” code that the alert does not
5250 point at.
53- - ** New code must comply with the analyzed coding standard.** Any code introduced by the
54- fix must itself satisfy every rule of the coding standard being verified
55- (e.g. all MISRA C rules). Do not limit the review to the rule targeted by
56- the alert: iterate over the full supported rules list for the applicable
57- standard and, for each rule, read its help file
58- (` <language>/<standard>/src/rules/<rule-id>/<rule-id>.md ` or ` .qhelp ` ) to
59- understand its intent, then verify that the introduced code does not violate
60- it. Cross-check against the COMPLIANT examples in each rule's
61- ` test/rules/<rule-id>/ ` directory.
62- - ** Match the project’s existing style.** Follow the conventions visible in
63- the surrounding source files (naming, headers, namespaces, C++ standard
64- level, use of ` enum class ` , etc.).
65- - ** Preserve behaviour.** A coding-standards fix is a refactor at the source
66- level, not a functional change. The fix must not alter observable runtime
67- behaviour unless the rule explicitly targets undefined or implementation-
68- defined behaviour that has to change.
69-
70- ### 3. Do not touch build outputs, generated files, or ` .gitignore `
51+ - ** Do not attempt at fixing design issues.** A fix should not attempt to
52+ “improve” the design of the code or address architectural issues.
53+ Always verify that the code section around the alert is intended to follow
54+ the standard and add a comment.
55+ The presence of certain design issues (e.g. dynamic memory allocation) might
56+ indicate that the code is not intended to be compliant with the standard, and
57+ that a deviation should be added instead of a code fix.
58+ - ** New code must comply with the same standard.** Any code introduced by the
59+ fix must itself satisfy the coding standard being verified (e.g. MISRA C++
60+ 2023). Cross-check the inserted code against the COMPLIANT examples in the
61+ corresponding ` test/rules/<rule-id>/ ` directory and against neighbouring
62+ rules that are obviously relevant (e.g. don’t fix an integer-conversion rule
63+ by introducing a cast that violates a different MISRA rule).
64+ - ** Preserve safe and desired functional behavior.** ensure the resulting code
65+ handles all reasonable real-world scenarios as the code originally intended.
66+ This may involve precisely maintaining the existing code behavior, or it may
67+ involve fixing subtle or rare bugs, for instance dangerous overflows,
68+ that the existing code does not handle and the rule is designed to detect.
69+ - ** Fix dangerous bugs** If the alert is flagging unsafe or undefined behavior,
70+ critically examine how that safety issue in the code should be properly fixed.
71+ Add detections and error handling if necessary to make the code safe under
72+ all conditions without introducing unnecessary complexity. Follow existing
73+ project guidelines on how to handle rare, dangerous, or unexpected scenarios
74+ that occur at runtime.
75+ - ** Thoroughly explain analysis and functional changes.** If the alert does not
76+ introduce any unwanted behavior and the change is functionally equivalent,
77+ explain your thinking, and clearly show that the code before was safe, and
78+ that the new code is exactly equivalent in behavior.
79+ If there was a dangerous edge case or condition, explain exactly how that
80+ scenario would create problems in the code and how the fix will prevent such
81+ issues and improve the safety and quality of the codebase.
82+
83+ ## 3. Do not touch build output, generated files, or ` .gitignore `
7184
7285Autofix pull requests must only change source files that are part of the
7386checked-in project. They must ** not** include:
@@ -80,29 +93,51 @@ checked-in project. They must **not** include:
8093 Suppression or scope changes must use the deviation mechanism (see §4),
8194 not workflow edits.
8295
83- ### 4. Deviations — respect project policy and reference it in fixes
96+ ## 4. Deviations — respect project policy and reference it in fixes
8497
85- A project under analysis may declare that a rule, file, region, or specific
86- construct is intentionally exempt from a coding standard. Such deviations are
98+ A project may declare that a rule, file, region, or specific construct is
99+ intentionally exempt from a coding standard. Such deviations are
87100not always expressed through the same mechanism: a project may use the
88- ** standard CodeQL Coding Standards deviation mechanism** , a ** custom
89- annotation or attribute** convention, ** in-source line / block comments** ,
101+ ** standard CodeQL Coding Standards deviation mechanism** , a
102+ ** custom annotation or attribute** convention,
103+ ** in-source line / block comments** ,
90104or a ** separate documentation file** (for example a ` DEVIATIONS.md ` ,
91- ` MISRA-deviations.md ` , compliance matrix, or similar).
105+ ` MISRA-deviations.md ` , ` coding-standards.yml ` , compliance matrix, or similar).
92106
93- If the alert location is covered by an existing deviation:
107+ Locate the deviations file and explicitly search for matching deviations
108+ before proposing code changes.
109+ The fix proposal must take what is found into account and treat it as an
110+ existing deviation if it clearly covers the alert location and rule.
94111
95- - ** Still propose a code fix ** that would make the location compliant by
96- default. Authors may have left the deviation in place pragmatically and
97- may prefer a real fix.
98- - ** In the pull request description, explicitly state ** that a matching
99- deviation already exists in the project, referring to the possible deviation marker
100- so reviewers can decide whether to accept the fix or keep the deviation.
101- - If the deviation style does not match the coding standards user manual, propose a fix.
112+ If the alert location is covered by an existing deviation:
113+ - Look for existing deviations of that rule, and see if any should apply
114+ - In the pull request description, explicitly state that a matching
115+ deviation already exists in the project, citing the file path and the
116+ relevant ` rule-id ` / ` query-id ` / ` permit-id ` / ` code-identifier ` / scope
117+ (paths or markers) so reviewers can decide whether to accept the fix or
118+ keep the deviation.
102119- Do not silently delete or weaken an existing deviation, permit, or
103120 re-categorization entry as part of the fix.
104-
105- ### 5. False positives — propose a deviation, do not stay silent
121+ - Propose a code fix that would make the location compliant by
122+ default. Authors may have left the deviation in place pragmatically and
123+ may prefer a real fix.
124+ - Consider whether an existing code identifier should be used
125+ - Consider whether a file-wide exception should be used
126+ - Consider whether a new code identifier should be used
127+ - If using a code-identifier, look for examples to determine whether
128+ to use [[ attribute]] form
129+ - If using an [[ attribute]] , look for project formatting configurations or code
130+ examples to determine how to format the attribute relative to its declaration
131+ - When using deviation comments, consider project formatting, the specific
132+ violation in question, and other example deviation comments in the project to
133+ determine whether to use same-line, next-line, or begin/end comment deviations
134+ Project formatting configuration may be .clang-format, etc.
135+
136+ ## 5. False positives — propose a deviation, do not stay silent
137+
138+ Precedence: if an alert is judged to be a false positive, the false-positive
139+ workflow in this section overrides any guidance above about proposing a code
140+ fix when a deviation exists.
106141
107142Copilot autofix normally refrains from opening a pull request when it
108143considers an alert to be a false positive. For CodeQL Coding Standards alerts
@@ -130,8 +165,8 @@ When an alert is judged to be a false positive, the autofix PR must:
130165 or directory is affected;
131166 - a project-wide deviation only when the rule is genuinely inapplicable to
132167 the project.
133- Use ` <standard> ` ∈ {` misra ` , ` autosar ` , ` cert ` } as appropriate for the
134- alert.
168+ Use ` <standard> ` ∈ {` misra ` , ` autosar ` , ` cert ` } as appropriate for the
169+ alert.
1351703 . ** Populate the deviation record** with at least:
136171 - ` rule-id ` matching the alert’s rule identifier;
137172 - ` query-id ` matching the alert’s ` @id ` (when the deviation is meant to
@@ -149,7 +184,7 @@ When an alert is judged to be a false positive, the autofix PR must:
1491845 . ** In the PR description** , explicitly state that the alert is being
150185 handled as a false positive via a deviation (not by code change), link to
151186 the
152- [ deviation mechanism documentation] ( https://github.com/github/codeql-coding-standards/blob/main/docs/user_manual.md#applying-deviations ) ,
187+ [ deviation mechanism documentation] ( https://github.com/github/codeql-coding-standards/blob/main/docs/user_manual.md#applying-deviations ) ,
153188 and summarise the justification so a reviewer can approve or reject it.
154189
155190A false-positive PR should therefore contain ** only** the deviation entry
0 commit comments