Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7017507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:55:24+00:00 2026-05-27T22:55:24+00:00

The following file foo.c is a simplified version of a subtler bug I found

  • 0

The following file foo.c is a simplified version of a subtler bug I found in my code.

int b;

void bar(int a);

void foo(int a)
{
  bar(a);
  a = 42;
}

The line a = 42 is in fact a typo in my code: I meant b = 42. I don’t expect the compiler to detect that I made a typo, but I would like the get a warning that I am assigning to a local variable (or a function parameter) that is not going to be used anymore. If I compile this file with

% gcc-4.6 -Wall -Wextra -pedantic -O3 -c foo.c

I get absolutely no warning. Inspecting the generated code shows that the assignment a = 42 is not performed, so gcc is perfectly well aware that this instruction is useless (hence potentially bogus). Commenting the call to bar(a); does produce a warning warning: parameter ‘a’ set but not used [-Wunused-but-set-parameter], so it seems like gcc will not warn as long as a is used somewhere in the function, even if it is before the assignment.

My questions:

  1. Is there a way to tell GCC or Clang to produce a warning for such case? (I could not get clang 3.0 to produce any warning, even with the call to bar removed.)
  2. Is there a reason for the actual behavior? I.e, some cases were it is actually desirable to assign to local variables that will be thrown away by the optimizer?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:55:25+00:00Added an answer on May 27, 2026 at 10:55 pm

    There is no gcc or clang option to my knowledge that can warn about this useless assignment.

    PC-Lint on the other hand is able to warn in this situation.

    Warning 438 Last value assigned to variable ‘Symbol’ not used — A value had
    been assigned to a variable that was not subsequently used. The
    message is issued either at a return statement or at the end of a
    block when the variable goes out of scope.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got following code: File: Foo.h class Foo { friend void Bar(); }; File:
Suppose you have the following file: textfield,datetimefield,numfield foo,2008-07-01 17:50:55.004688,1 bar,2008-07-02 17:50:55.004688,2 The Ruby code
I think the following code is normal (and malloc/free is similar): int foo(){ FILE
Let's assume I have the following file structure: data.py foo = [] bar =
I'm applying an XSLT stylesheet to the following XML file: <top xmlns=http://www.foo.com/bar> <elementA />
I have the following (simplified) code: var Foo = (function () { var data
Imagine I wish to create (or overwrite) the following file :- C:\Temp\Bar\Foo\Test.txt Using the
The situation is the following: Macro Foo is defined in File foo.vss Macro Bar
I compiled the following four lines of code into a file called foo.exe, hoping
I'd like to do the following: cdef extern from foo.h namespace foo: int bar(int,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.