Musashi Posted November 13, 2019 Share Posted November 13, 2019 It is not completely uncommon, e.g. in beginner scripts, that variables will be declared within a conditional statement (more precisely in only one part of the statement). A standard hint is to use Opt("MustDeclareVars", 1) or even #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 6 -w 7 According to the help, the parameter -d should be identical with Opt("MustDeclareVars", 1). In the following script, however, these two alternatives show a different behavior when you declare a variable inside a conditional statement : ; ------ > Comment out 1. or 2. ; 1. #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 6 -w 7 ; Info : -d as Opt("MustDeclareVars", 1) ; 2. ;~ Opt("MustDeclareVars", 1) ; Info : 1=Vars must be declared ConsoleWrite("+ ===> _Example(True) : " & @CRLF) _Example(True) ConsoleWrite("+ ===> _Example(False) : " & @CRLF) _Example(False) Func _Example($bValue) ; Local declaration of $sString inside If : If $bValue Then Local $sString = "Text IF" Else $sString = "Text ELSE" EndIf ConsoleWrite("> bValue = " & $bValue & " sString = " & $sString & @CRLF & @CRLF) EndFunc ;==>_Example --> 1. Console output #AutoIt3Wrapper_Au3Check_Parameters : + ===> _Example(True) : > bValue = True sString = Text IF + ===> _Example(False) : > bValue = False sString = Text ELSE +>23:50:49 AutoIt3.exe ended.rc:0 --> 2. Console output Opt("MustDeclareVars", 1) : + ===> _Example(True) : > bValue = True sString = Text IF + ===> _Example(False) : "C:\BS_AutoIt\Projekte\Testprogramme\MustDeclare-TEST.au3" (50) : ==> Variable used without being declared.: $sString = "Text ELSE" ^ ERROR ->23:59:19 AutoIt3.exe ended.rc:1 ==> This happens when starting the script with Run (F5). Compile and Build do not show any warnings. Using just $g_sStr = "" without declaration generates the warning 'possibly not declared/created yet' in both variants. ;~ #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 6 -w 7 Opt("MustDeclareVars", 1) $g_sStr = "" Did I miss something? "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
seadoggie01 Posted November 14, 2019 Share Posted November 14, 2019 Au3Check doesn't run the same algorithm to check variable declarations as the compiler. Au3 check attempts to find any errors that the compiler does, but it's obviously not perfect. Musashi 1 All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Spoiler My Humble Contributions:Personal Function Documentation - A personal HelpFile for your functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types Link to comment Share on other sites More sharing options...
Musashi Posted November 14, 2019 Author Share Posted November 14, 2019 3 hours ago, seadoggie01 said: Au3Check doesn't run the same algorithm to check variable declarations as the compiler. Au3 check attempts to find any errors that the compiler does, but it's obviously not perfect. Thank you, seadoggie01 ! It was also my assumption, that two different algorithms will be applied. A little bit of a background : I am currently helping a user in a private conversation of the DE-forum. The code he delivered is filled with things, like : Some variables are declared, others are not Global declarations within functions Global and Local declarations within loops and/or conditional statements etc. My advice was, that he should insert #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 6 -w 7 and clean up the code until no more warnings appear. Thereby I encountered the behaviour described above. "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
BrewManNH Posted November 14, 2019 Share Posted November 14, 2019 (edited) $sString isn't declared if the first check fails, so that's why you get the warning. Func _Example($bValue) ; Local declaration of $sString inside If : Local $sString If $bValue Then ;~ Local $sString = "Text IF" ; <<<<<<<<<<<<< don't declare a variable like this, declare it first then use it later $sString = "Text IF" Else $sString = "Text ELSE" EndIf ConsoleWrite("> bValue = " & $bValue & " sString = " & $sString & @CRLF & @CRLF) EndFunc ;==>_Example That's how it should be done. Edited November 14, 2019 by BrewManNH If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
Musashi Posted November 14, 2019 Author Share Posted November 14, 2019 (edited) 46 minutes ago, BrewManNH said: $sString isn't declared if the first check fails, so that's why you get the warning. That's how it should be done. Thanks for your reply. I know how it should be done . The declaration of the local variable within If $bValue Then Local $sString = "Text IF" Else $sString = "Text ELSE" EndIf was intentionally chosen incorrect. The issue I wanted to point out is that Opt("MustDeclareVars", 1) gives a warning but .. Au3Check_Parameters=-d does not, when you Run the script with F5 (see console outputs in Post #1). The reason is most likely : 8 hours ago, seadoggie01 said: Au3Check doesn't run the same algorithm to check variable declarations as the compiler Edited November 14, 2019 by Musashi "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
Nine Posted November 14, 2019 Share Posted November 14, 2019 So the conclusion is to use Opt ("MustDeclareVars", 1) all the time, even if Au3Check_Parameters=-d is set. mLipok and Musashi 2 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
BrewManNH Posted November 14, 2019 Share Posted November 14, 2019 7 minutes ago, Musashi said: The issue I wanted to point out is that Opt("MustDeclareVars", 1) gives a warning but .. Au3Check_Parameters=-d does not The problem is that Au3Check reads the script from top to bottom and sees that it's declared in the line above it, so it doesn't error. The option is used when the script is being ran, which is not how Au3Check works. The interpreter actually reads the script as it goes through the logic of the script, not how it is written. By the logic of the script, the variable isn't declared, by how it's written Au3Check assumes it is. It's a check of the script, not a debugger. Musashi 1 If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
Musashi Posted November 14, 2019 Author Share Posted November 14, 2019 54 minutes ago, Nine said: So the conclusion is to use Opt ("MustDeclareVars", 1) all the time, even if Au3Check_Parameters=-d is set. In short, yes . I would generally recommend to use Opt("MustDeclareVars", 1), even if it can be a bit annoying sometimes. Many people don't like to add this function retroactively to their scripts, because it usually results in a lot of warnings. At least in new scripts this function should always be implemented (helpful on the long run). @BrewManNH : Thank you for the deeper insight into the functionality of Au3Check. By the way : My contribution was not meant as criticism or bug report, only as a hint . "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now