AppCode Tips & Tricks

Polish Your Code with AppCode’s Inspections and Intentions

AppCode is constantly analyzing your code to find parts that can be improved. It provides sets of code inspections and intention actions for each supported language for this exact purpose.
Inspections locate unused or redundant code, syntax errors, probable bugs, spelling mistakes, obsolete or redundant code constructs, in other words, everything that you may want to revise before compiling or committing your code. For most issues detected by the inspections, the IDE will provide quick-fixes that will help you repair the problems with a single click.

Intention actions suggest ways to alter specific code constructs to make them more compact or clear, quickly fix issues, transform expressions and control flow statements, and so on.

Apply context actions

Intention actions and inspection quick-fixes together are called context actions. If there are any context actions available for the code at the caret, you will see a yellow or red bulb in the editor. To apply an action, press ⌥⏎, select it from the list, and press :

Apply intention actions

When fixing a problem detected by an inspection, you can apply the quick-fix to all the same problems in the current file:

Apply a quick-fix to a file

Inspection widget and Problems tool window

In the top-right corner of the editor window, you may see the Inspection Widget that will appear if any issues are detected by the enabled inspections in the current file:

Inspection widget

To jump between the issues, click the up and down arrows in the widget. To see the full list of issues, click the widget. It will open the Problems tool window:

Problem tool window

By clicking a problem description in the tool window, you can jump to the related code in the editor and apply one of the available quick-fixes (⌥⏎). If you want to see the fix before applying it, open the preview (⌥Space):

Quick-fix preview

Running inspections on demand

Instead of fixing issues sporadically in the files you are working on, you may sometimes need to clean up your codebase, like when getting rid of unused or obsolete code. In AppCode, you can run a set of inspections for a specified scope of files or a single inspection.

Run a set of inspections

Select Code | Inspect Code from the main menu. In the dialog that opens, choose the files to be checked: the whole project, the current file, or a custom scope. When selecting the Custom scope option, you can choose one of the predefined scopes. For example, you can run inspections in only the files that have been recently changed:

Custom scope

If you want to check a specific set of files or locations, you can create a new scope by clicking next to the Custom scope field.

The set of inspections to run can be modified in the Inspection profile section. Click and either enable/disable inspections in the current profile or create a new profile and enable only the necessary inspections you’d like to have.

To create a new profile, just duplicate the current one:

Duplicate an inspection profile

In the new profile, you can deselect all inspections by clicking on the toolbar and selecting only the ones that you need.

Note: Code analysis is one of the most time-consuming features in the IDE, but you can speed it up by disabling any unnecessary inspections in the selected profile.

Run a single inspection

To run a specific inspection by its name, select Code | Analyze Code | Run Inspection by Name (or press ⌥⇧⌘I). Start typing the name and select an inspection for your file type or programming language from the list:

Run a single inspection by its name

Afterwards, you can define the inspection scope and set up additional inspection options if needed.

Disable inspections and intentions

If you don’t want to be distracted by any of the intention actions, you can disable them directly from the editor. Just click the arrow next to the action name and select Disable ‘Action name’:

Disable intentions

Similarly, you can disable inspections:

Disable inspection

Alternatively, you can find an intention action or inspection that you want to disable in Preferences and disable it there in the list:

Disable inspections in Preferences

Swift inspections

Here is a short overview of the inspections that we currently have for Swift. Go to Preferences | Editor | Inspections | Swift to see the full list:

Swift inspections

All of AppCode’s Swift inspections can be divided into the following groups:

Inspections that detect redundant code elements

AppCode detects symbols that can be removed without breaking the code syntax, such as return in single-expression functions, excessive usages of self, redundant parentheses in the arithmetic or boolean expressions, and so on. All of these inspections provide quick-fixes that allow you to safely remove any unnecessary elements:

Remove redundant elements

Localization inspections

If your application supports several languages, the AppCode localization inspections may come in handy. The Missing localization inspection analyzes your Swift code and finds the localization keys that are not added to the .string files:

Localization inspections

There is also a set of inspections that work in the .strings files (Preferences | Editor | Inspections | Strings files). They detect duplicate and unused localization keys, along with keys that are missing in some languages.

SourceKit inspections

With the SourceKit inspections enabled, AppCode finds the parts of your Swift code that can be improved or may lead to compilation errors and highlights them (for example, syntax errors, incorrect usages of variables and constants, unhandled errors, and so on). Hover the cursor over the highlighted code element to see the error details. For some of these inspections, you can also apply SourceKit fix-its (press ⌥⏎ and click Apply Fix-it):

SourceKit inspections

Swift intention actions

A full list of the Swift intention actions is available under AppCode | Preferences | Editor | Intentions | Swift:

Swift intention actions

Let’s take a look at the most common use cases.

Declarations

With ⌥⏎, you can add an explicit type to a declaration or remove it and split or join declarations and assignments:

Declaration intentions

Strings

With intention actions applied to the string literals, you can localize your strings, make them multi-line or single-line, and increase or decrease the spring escape level:

Strings intentions

Closures

You can convert a closure argument to a trailing closure and vice versa:

Closure intentions

Operators

When working with boolean expressions, you can quickly negate operators, flip operands, replace || with && applying De Morgan’s laws, and more:

Operators intentions

Control flow

In if, guard, switch, and other control flow statements, there are plenty of intention actions available. For example, you can quickly invert if statements, replace if with guard, add missing switch cases, and so on:

Control flow intentions

Summary

The list of Swift inspections and intention actions is constantly growing. We are trying to cover as many use cases as possible to make your coding process smoother and easier. If you have ideas for new inspections or intentions, please don’t hesitate to submit an issue in our project tracker, YouTrack. You can also vote for an existing one there. For more information about inspections and intention actions, go to the Code inspections and Intention actions sections of our online help.

Happy development!
Your AppCode team