How to get a compiler error for assignments in an if statement?

Thomas Weller

Is it possible in Visual Studio to get a compiler error for an assignment in an if statement? How?

#include <iostream>
int main()
{
    int a = 2;
    if (a = 3)  // Want a warning here
        std::cout << "Avoid this!\n";
}

I know I can switch to Yoda conditions (if (3=a)), but I really don't want to.

I tried: setting the warning level to /Wall but I still don't get a warning that I could then treat as an error.

I am doing this in Visual Studio 2019 (16.11.19).:

Visual Studio 2019

The build output is

Rebuild started...
1>------ Rebuild All started: Project: Yoda2019, Configuration: Debug Win32 ------
1>Yoda2019.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\limits.h(70,5): error C2220: the following warning is treated as an error
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\limits.h(70,5): warning C4668: '__STDC_WANT_SECURE_LIB__' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(154,5): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(164,5): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\atomic(284,9): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\atomic(299,9): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\atomic(315,9): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\atomic(375,9): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>Done building project "Yoda2019.vcxproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

and Visual Studio 2022 (17.4.0)

Visual Studio 2022

The build output is

Rebuild started...
1>------ Rebuild All started: Project: YodaCheck, Configuration: Debug x64 ------
1>YodaCheck.cpp
1>YodaCheck.vcxproj -> B:\Projekte\C++\Dynamic Linking\YodaCheck\x64\Debug\YodaCheck.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
========== Elapsed 00:01,379 ==========

so I see no warning which I could convert into an error.

Adrian Mole

You can turn any specific warning into an error, using the #pragma warning(error:nnnn) directive. In your case, the warning is:

warning C4706: assignment within conditional expression

So, adding the relevant #pragma directive to the code will generate a compiler error:

#include <iostream>
#pragma warning(error:4706)

int main()
{
    int a = 2;
    if (a = 3)  // Want a warning here
        std::cout << "Avoid this!\n";
}

This now gives:

error C4706: assignment within conditional expression

For a solution wide setting, change it here in the property pages:

C4706 in property page

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get the line number of GLSL compiler error

Is this Java compiler error or correct statement?

How to get a list of assignments and display it in the workflow form?

How to get "at most once" semantics in variable assignments?

How to get a list with all possible assignments for workers?

How to solve error regarding $_GET["view"] in IF STATEMENT?

How to get proper error in catch statement?

C Assignments In IF Statement

How to get the preprocessed line which trigger an internal compiler error?

Why Kotlin compiler gives an error in a valid IF statement?

No compiler error on using garbage characters in #include statement

Defining a block in a switch statement results in a compiler error

Compiler error `assigning incompatible type within if statement`

Java Compiler Error: Missing Return Statement

How is throw statement inside catch block preventing compiler error for uninitialized variable?

Why does a Java Compiler not produce an unreachable statement error for an unreachable then statement?

How to fix FlowJS error for dynamic Boolean variable assignments?

using statement for multiple assignments to the variable

How can I get a list of Class Assignments using the Graph API?

How to represent Get error type using a switch statement?

C++ compiler 'shallow' copies and assignments

Compiler optimizations and temporary assignments in C and C++

How to throw an error in the LESS compiler

How to get the java compiler for ubuntu?

How to get compiler version symbol

Force compiler to emit an error when not all implementations are covered in "when" statement

Why does Java have an "unreachable statement" compiler error?

Why doesn't the compiler throw an error saying "No return statement"?

Why does the statement "2i;" NOT cause a compiler error?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive