F# Suppress Warnings - f#

Sometimes I get annoying pattern matching and indent warnings when compiling F#. Is there a way to disable warnings? I'm pretty OCD over warnings.

In case you forget, you can type
let rec x = lazy(x.Value)
and get the warning
This and other recursive references to
the object(s) being defined will be
checked for initialization-soundness
at runtime through the use of a
delayed reference. This is because you
are defining one or more recursive
objects, rather than recursive
functions. This warning may be
suppressed by using '#nowarn "40"' or
'--nowarn:40'.
which shows that you can use either the compiler flag --nowarn on the command-line, or use the hash-directive #nowarn in your code. The warning number for each warning will be part of the build output (the Visual Studio error list does not display the numbers, so if in VS, build and then inspect the build output). Also, if inside VS, you can go to the project properties page, "Build" tab, and use the "warning level" selector and "suppress warnings" field (a semicolon-delimited list of numbers) to control which warnings are diplayed via the VS UI.
(BTW, I believe #nowarn only turns off the warning in the current file, whereas --nowarn turns it off for the entire project being compiled.)

See: Compiler Options (F#)
--nowarn:<int-list>:
Disable specific warnings listed by
number. Separate each warning number
by a comma. You can discover the
warning number for any warning from
the compilation output.
This compiler option is equivalent to
the C# compiler option of the same
name. For more information, see
/nowarn (C# Compiler Options).

Related

Prevent ArmClang to add calls to Standard C library

I am evaluating Keil Microvision IDE on STM32H753.
I am doing compiler comparison between ARMCC5 and AC6 in the different optimisation levels. AC6 is based on Clang.
My code is not using memcpy and I have unchecked "Use MicroLIB" in the project settings , However a basic byte per byte copy loop in my code is replaced by a memcpy with AC6 (only in "high" optimisation levels). It doesn't happen with ARMCC5.
I tried using compilation options to avoid that, as described here: -ffreestanding and -disable-simplify-libcalls, at both compiler and linker levels but it didn't change (for the second option, I get an error message saying that the option is not supported).
In the ARMCLANG reference guide i've found the options -nostdlib -nostdlibinc that prevent (??) the compiler to use any function of a standard lib.
However I still need the math.h function.
Do you know how to prevent clang to use functions from the Standard C Lib that are not explicitely called in the code ?
EDIT: here is a quick and dirty reproduceable example:
https://godbolt.org/z/AX8_WV
Please do not discuss the quality of this example, I know it is dumb !!, I know about memset, etc... It is just to understand the issue
gcc know a lot about the memcpy, memset and similar functions and even they are called "the builtin functions". If you do not want those functions to be used by default just use the command line option -fno-builtin
https://godbolt.org/z/a42m4j

Getter/setter "is used reflectively but not in MirrorsUsed" warning after dart2js compilation

In my class, I have getters and setters which are working thanks to NoSuchMethod - that is, they're not explicitly defined. After I compile and run the js, I get the following error in the browser console:
Warning: 'closes_in=' is used reflectively but not in MirrorsUsed. This will break minified code.
In this case, closes_in= is an example of such a setter and there are some other warnings related to other getters/setters too. I do have a #MirrorsUsed with the name of the library/class included and the resulting compiled js is actually smaller than it was without using #MirrorsUsed statements.
However, if a -m flag is passed to dart2js, then when the js program runs it fails - as predicted by the warning message.
Thus, I have two questions:
1. How do I write my #MirrorsUsed statement so that the warning goes away?
2. If it's not possible, how do I suppress the warning message? (because if it's not possible to solve the problem, then my only option would be to NOT minify the file then).

How to identify redefined variables or shadowed variables

When using the same variable twice in the same scope with the F# compiler there is no warning or feedback. e.g.
let s = "abc"
let s = "def"
printfn "%A" s
results in
def
I have seen
Is there a way to have warnings for shadowing values in F# in Visual Studio?
F# value shadowing - is it possible to disable value shadowing within the same scope
Is there a way to get feedback about shadowed variables either by a compiler warning or visually in the editor. How can this be done?
First off, shadowing of variables in the same scope is not a bug or something that should be disabled. As Joel Mueller states it is legitimate, useful, and common.
Per MSDN
At any level of scope other than module scope, it is not an error to
reuse a value or function name. If you reuse a name, the name declared
later shadows the name declared earlier.
The Syntax Coloring feature of the Visual Studio extension F# Power Tools will highlight the current valid variable and show the shadowed variables as a light grey. e.g.
The extension can be installed from Visual Studio menu
Tools -> Extensions and Updates
Once the dialog opens
Select Visual Studio Gallery
In the upper right search box enter F# Power Tools
Since I have already installed it, the option to install is not shown.
The feature can be activated from the Visual Studio menu
Tools -> Options -> F# Power Tools -> General -> Syntax Coloring -> Grey out unused declarations
With option off:
with option on:
Note: After changing the option the source file(s) must be closed and then reopened for the change to take effect. Visual Studio does not need to be restarted for this but doing so will have the same effect.
Thanks to Ringil for noting my earlier invalid statement.
Note from source code:
Graying out unused declarations
Currently unused non public types, methods, functions and values declarations are checked. Beware that this
feature is only 100% reliable when the code has no type error. This
setting is available in General options. It is disabled by default
because there might be performance issues on large files.
F# Power Tools features list

Defining preprocessor symbols for CLion analyzer

In my project there's a file enclosed in an ifdef preprocessor directive
#ifdef SOME_SYMBOL
... entire file ...
#endif
SOME_SYMBOL is defined by another file that's compiled before this one, and the code works as expected, but the static analyzer isn't aware of this symbol and so it treats SOME_SYMBOL is undefined. The entire file has no syntax highlighting and some of the analysis is just skipped (e.g. syntax error highlighting).
Is there a way to tell the analyzer to treat this symbol as defined without defining it in CMakeLists.txt?
I don't have the option of defining SOME_SYMBOL in CMakeLists.txt since the project depends on it being undefined in some compilation paths (changing this would be near impossible).
Update:
Seems like this is currently an open issue with JetBrains. See Issue CPP-2286
Clion now has a macro which you can use to detect the IDE:
https://youtrack.jetbrains.com/issue/CPP-1296#comment=27-1846360
#ifdef __JETBRAINS_IDE__
// Stuff that only clion will see goes here
#endif
This allows you to put in defines to make clion render your code properly in cases where it can't be clever enough to figure it out.
The __JETBRAINS_IDE__ macro's value is a version string for the IDE. Specific versions of the macro exist for different Jetbrains IDEs: __CLION_IDE__, __STUDIO_IDE__ (for Android Studio), and __APPCODE_IDE__ (for AppCode).
Yay!
To get syntax highlighting:
Go to Settings ⇒ Editor ⇒ Colors&Fonts ⇒ C/C++ and remove all ticks for 'Conditionally non-compiled code'. This way all code will show up with the usual highlighting.
The task has no solution for common case.
But! You can find the target and related resolve context, where SOME_SYMBOL is defined.
...in the status bar you can find the Resolve Context chooser for switching between the Debug, Release, RelWithDebInfo and MinSizeRel contexts to resolve your code in the IDE with the desired definitions.

F# value shadowing - is it possible to disable value shadowing within the same scope

I spoted a bug in my own code because of copy/paste. The same value name is shadowned by the copy/pasted in the same scope.
let func() =
let a = 1
let a = something_else
....
In C# I wont pass compile. is there a way to disable shadowing? at least within the same level of scope?
Thanks
You can't disable shadowing in F# -- it's an important language feature.
However, you can instruct the compiler to issue a warning or error which will help you catch cases of accidental shadowing.
In the project properties, add --warnon:1182 to the "Other flags" textbox (on the Build tab, under the platform target dropdown). This triggers a compiler warning when you accidentally shadow a variable and cause it not to be used anywhere. If you'd rather these cases cause compilation to fail, you can also add 1182 to the "Specific warnings" textbox under the "Treat warnings as errors" section of the Build tab.
Finally -- do install the Visual F# Power Tools extension. It provides additional syntax highlighting functionality and will indicate unused variables so they're easy to spot in your code.

Resources