Modify Existing Code Analyzer Checks or Create New Checks
The Code Analyzer checks code and reports errors, warnings, and informational messages. You can modify existing checks or create new checks by configuring the Code Analyzer. For example, you can disable checks, change their severity, or change their message text. You can also check for specified functions or variables by creating custom checks.
To configure checks, create a file named
codeAnalyzerConfiguration.json and add a
"checks" property. In the "checks" property,
specify existing or new check IDs and their corresponding properties. Common properties
include "enabled", "severity", and
"messageText". For more information on setting up this
configuration file, see Customize Code Analyzer Checks Using Configuration File.
For a full list of checks that you can configure, see Index of Code Analyzer Checks. To get a list of
the code issues flagged in certain files and the corresponding check IDs, use codeIssues.
Enable or Disable Checks
You can enable or disable Code Analyzer checks by using the
"enabled" property. Most checks can be disabled.
| Property Name | Description | Example |
|---|---|---|
"enabled" (optional) | Specify true or false to
indicate whether a check is enabled in the MATLAB® Editor. |
"enabled": false |
For example, enable one existing Code Analyzer check and disable another by
specifying this code in the codeAnalyzerConfiguration.json
file.
"checks": {
"DAFPV": {
// This check is disabled by default
// Enable to warn about use of persistent variables
"enabled": true
},
"DEFNU": {
// This check is enabled by default
// Disable to ignore unused function warnings
"enabled" : false
}
} Change Severity of Checks
You can change the severity of Code Analyzer checks by using the
"severity" property.
| Property Name | Description | Example |
|---|---|---|
"severity" (optional) | Specify the check severity as "warning",
"error", or "info".
Messages that have been changed to "error" do not
prevent code execution. |
"severity" : "error" |
Note
Changing the severity of a Code Analyzer message does not affect code
execution. Code is still executed even if the severity of a check has been set
to "error". However, syntax errors continue to prevent
execution even if their severity is changed or the message disabled.
For example, increase the severity of three existing Code Analyzer checks by
specifying this code in the codeAnalyzerConfiguration.json
file.
"checks": {
"EVLCS": {
// "eval" family of functions is slow and unclear
"severity": "error"
},
"NOANS": {
// Do not use "ans" as a variable
"severity": "error"
},
"CHAIN": {
// Chained logical operations like "a > b > c" are usually a bug
// For scalars, it should be: "(a > b) && (b > c)"
// For arrays, it should be: "(a > b) & (b > c)"
"severity": "error"
}
} Change Message Text
You can customize the message text displayed for a Code Analyzer check by using
the "messageText" property.
| Property Name | Description | Example |
|---|---|---|
"messageText" (optional) | Specify the text to display in the Code Analyzer message. |
"messageText" : "Do not use global variables!" |
For example, change the message text displayed after increasing the severity of an
existing check by specifying this code in the
codeAnalyzerConfiguration.json file.
"checks":
{
"GVMIS":
{
// Do not use global variables
"severity": "error",
"messageText": "Do not use global variables!"
}
}Specify Maximum Input and Output Arguments
You can configure the maximum number of input and output arguments in functions.
Use the "limit" property of the check "FCNIL"
to specify the maximum number of input arguments. Use the "limit"
property of the check "FCNOL" to specify the maximum number of
output arguments.
| Property Name | Description | Example |
|---|---|---|
"limit" (optional) | For the "FCNIL" and "FCNOL"
checks, specify the maximum number of arguments. |
"limit" : 10 |
For example, disallow functions with more than 15 output
arguments by specifying this code in the
codeAnalyzerConfiguration.json file.
"checks": {
"FCNOL": {
// Disallow too many outputs
"limit": 15,
"severity": "error",
"messageText": "Too many outputs.",
"enabled": true
}
} Specify Maximum Characters Per Line
You can configure the maximum number of characters per line (including white-space
characters and comments) by setting the "limit" property of the
check "LLMNC".
For example, disallow lines with more than 75 characters by
specifying this code in the codeAnalyzerConfiguration.json
file.
"checks": {
"LLMNC": {
// Disallow long lines with more than 75 characters
"limit": 75,
"severity": "error",
"messageText": "Number of characters per line exceeds 75.",
"enabled": true
}
} Specify Maximum Lines in a Function
You can configure the maximum number of lines of code in a function by setting the
"limit" property of the check
"FCNLL".
For example, issue a warning for functions with more than 80
lines of code by specifying this code in the
codeAnalyzerConfiguration.json file.
"checks": {
"FCNLL": {
// Issue a warning for functions with more than 80 lines of code
"limit": 80,
"severity": "warning",
"enabled": true
}
} Specify Maximum Levels of Nested Control Statements
You can configure the maximum number of nesting levels for control statements,
such as for and if, by setting the
"limit" property of the check
"MNCSN".
For example, issue a warning for control statements that are nested more than
4 levels deep by specifying this code in the
codeAnalyzerConfiguration.json file.
"checks": {
"MNCSN": {
// Issue a warning for deeply nested (>4) control statements
"limit": 4,
"severity": "warning",
"enabled": true
}
} Create Checks for Specified Functions
You can configure the Code Analyzer to create custom checks for specified
functions. To create a custom check for functions, first specify a custom check ID.
The custom check ID must be a valid MATLAB identifier. Then, define the
"rule" property of the custom check.
| Property Name | Description | Example |
|---|---|---|
"rule" | Define the rules for the custom check. Specify the
|
"rule": {
"template": "functionCall",
"functionNames" : "evalin"
} |
For example, create a custom check that reports an error for
evalin function calls by specifying this code in the
codeAnalyzerConfiguration.json file. The custom check ID is
"MyFunctionCheck".
"checks": {
"MyFunctionCheck": {
// Disallow use of evalin function
"rule": {
"template": "functionCall",
"functionNames": "evalin"
},
"severity": "error",
"messageText": "Do not use evalin.",
"enabled": true
}
} Create Checks for Specified Variables
You can configure the Code Analyzer to create custom checks for specified
variables. To create a custom check for variables, first specify a custom check ID.
The custom check ID must be a valid MATLAB identifier. Then define the
"rule" property of the custom check.
| Property Name | Description | Example |
|---|---|---|
"rule" | Define the rules for the custom check. Specify the
|
"rule": {
"template": "variableName",
"variableNames": ["size","error","length"]
} |
For example, create a custom check that issues a warning when variables are
created with certain function names by specifying this code in the
codeAnalyzerConfiguration.json file. The custom check ID is
"MyVariableCheck".
"checks": {
"MyVariableCheck": {
// Disallow use of certain function names for variables
"rule": {
"template": "variableName",
"variableNames": ["size","error","length"]
},
"severity": "warning",
"messageText": "Avoid using function names for variables.",
"enabled": true
}
}