WarnAsError or A_Warn variable

Propose new features and changes
mnaoumov
Posts: 4
Joined: 11 Apr 2020, 14:18
Contact:

WarnAsError or A_Warn variable

11 Apr 2020, 14:56

I would like to be able to exit the script if any warnings occurred.

It could be done via some flag

Code: Select all

#Warn WarnAsError
#Warn ForceExit
Alternatively, for more control, we could have an automatic variable

Code: Select all

if (A_Warn) {
    ExitApp 1
}
User avatar
lmstearn
Posts: 694
Joined: 11 Aug 2016, 02:32
Contact:

Re: WarnAsError or A_Warn variable

12 Apr 2020, 02:10

Hi @mnaoumov , and welcome. :)
One possibility is to write the warnings to a file, and run a timer checking the existence of temp.ahk contents:

Code: Select all

FileAppend % "#Warn All, StdOut`na:=b+1", temp.ahk
There's also #ErrorStdOut where, similarly, Log.txt can be checked for zero contents:

Code: Select all

"C:\Program Files\AutoHotkey\AutoHotkey.exe" /ErrorStdOut "My Script.ahk" 2>"Syntax-Error Log.txt"
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: WarnAsError or A_Warn variable

13 Apr 2020, 01:53

This will be the default for AHK v2.
For now you can wrap code you execute in try and throw an exception in the related catch statement.

Code: Select all

Try {
	;your code here
	DllCall("IDon'tExist")
} Catch e {
	Throw e
}
Recommends AHK Studio
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: WarnAsError or A_Warn variable

13 Apr 2020, 03:08

@nnnik, you can't catch warnings.

I think something like #Warn ForceExit might be useful for v1. I don't think a_warn makes much sense, warnings are for mistakes you don't anticipate.

Cheers.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: WarnAsError or A_Warn variable

15 Apr 2020, 08:20

Oops - mixed up warnings and errors.
Recommends AHK Studio
mnaoumov
Posts: 4
Joined: 11 Apr 2020, 14:18
Contact:

Re: WarnAsError or A_Warn variable

15 Apr 2020, 17:53

Also, it seems that warning analysis is done during runtime instead of the parsing phase

Code: Select all

#Warn All, StdOut

if (A_Year == 2021) {
    someVariable := variableThatDoesNotExist
}
This code doesn't throw warnings now but will fail in 2021
Warning analysis has to be improved
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: WarnAsError or A_Warn variable

15 Apr 2020, 23:00

Also, it seems that warning analysis is done during runtime instead of the parsing phase
UseUnset (and UseEnv) is not feasible to detect at load time. The other warnings are made during load time. This is documented,
#warn wrote: UseUnsetLocal or UseUnsetGlobal: Warn when a variable is read
UseEnv: Warn when an environment variable is automatically used in place of an empty script variable
LocalSameAsGlobal: Before the script starts to run, display a warning for each undeclared local variable which has the same name as a global variable
ClassOverwrite [v1.1.27+]: Before the script starts to run, show a warning for each assignment targetting a class variable
Cheers.
mnaoumov
Posts: 4
Joined: 11 Apr 2020, 14:18
Contact:

Re: WarnAsError or A_Warn variable

17 Apr 2020, 11:11

@Helgef thanks, I did not read the documentation carefully. I missed that part of it. However, as a C# programmer, I am used to the idea of as many checks are done as early as possible. That's why this #Warn behavior upsets me
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: WarnAsError or A_Warn variable

17 Apr 2020, 11:35

I see. Note that variables in ahk exist simply by being written anywhere in the script, useunset doesn't warn against using a variableThatDoesNotExist, it warns when you read a variable which haven't been assigned a value. This is not possible to detect at load time, unlike a variable which haven't been declared in a language like c#. Consider,

Code: Select all

F() ? X := 1 : ""
Y := X
You can't know, in generally, what F() will return at load time, so the warning will have to wait until run time, if appropriate.

Cheers.
mnaoumov
Posts: 4
Joined: 11 Apr 2020, 14:18
Contact:

Re: WarnAsError or A_Warn variable

17 Apr 2020, 11:48

@Helgef Also what annoys me the most, is the following code doesn't throw any warnings

Code: Select all

myVar := new ClassThatDoesNotExist()
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: WarnAsError or A_Warn variable

17 Apr 2020, 11:58

That, and many other things, have been fixed in v2 (alpha) ;)

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 38 guests