It can be enabled in the Arduino IDE advanced settings.
The reason it is disabled by default is that the system is prone to occasional spurious "problem" detections. During the beta testing phase of the Arduino IDE 2.x development when it was enabled by report, we received a significant number of reports from less experienced users who were alarmed and distracted by the appearance of these diagnostic markers. For this reason, the decision was made to disable the feature by default.
Some of these spurious detections are the result of corner case mismatches in behavior between the Clang C++ compiler used by the language server that produces the diagnostics and the GCC compiler that is used to compile the sketch programs. An even more significant cause of spurious detections is that, in order to reduce the processing resource usage of the Arduino IDE application, a deferred library discovery system is used. This means that, between the time you add a new library to a sketch and the first time you compile the sketch after that, all references to objects from that library will produce diagnostics due to the path to the library not yet having been added to the compiler's "search path".
Even though it might not be suitable for a new user, a more experienced user has the ability to evaluate each of the problem detections and differentiate the legitimate ones from the spurious ones that should be ignored. I'll provide instruction you can follow to enable the feature:
- Press the Ctrl+Shift+P keyboard shortcut (Command+Shift+P for macOS users) to open the "Command Palette".
A menu will appear on the editor toolbar:

- Select the "Preferences: Open Settings (UI)" command from the menu.
ⓘ You can scroll down through the list of commands to find it or type the name in the field.
A "Preferences" tab will open in the Arduino IDE main panel. - Type
arduino.language.realTimeDiagnosticsin the "Search Settings" field of the "Preferences" tab. - Check the box under the "Arduino › Language: Real Time Diagnostics" setting.
- Close the Preferences tab by clicking its X icon.
You can put them under the src subfolder of the sketch.