Compiler warning question..

I have this method in a library..

// If  not full, add a charactor and update the state.
bool textBuff::addChar(char inChar) {

	char	oldestChar;
	
	if (full() && overwrite) {		// If we're full and overwrite is true..
		oldestChar = readChar();	// Make room by dumping the oldest char.
	}
	if (!full()) {						// If not full..
		buff[tail] = inChar;			// Save off the incoming char.
		inc(&tail);						// Increment the tail pointer.
		isFull = tail==head;			// Check and note if we are full.
		return true;					// Return a success.
	}
	return false;						// Couldn't save the new char, return a failure.
}

The compiler tosses up a warning about setting but not using "oldestChar".

Now, myself? I don't care. But someone using this stuff may get worried thinking its an error. I need to not have this toss a waring. What's the accepted way to deal with something like this? Just not have anything to return the char to?

-jim lee

What is its purpose?

It might help if you post the entire warning. Paraphrasing leaves out information.

You create and give oldestChar a value, but never use it after that. Why? I mean, the program would work exactly the same without it.

I agree, there seems to be no way you could use it for anything.

Here's the warning..

/Users/jimlee/Documents/Arduino/libraries/LC_baseTools/textBuff.cpp: In member function 'bool textBuff::addChar(char)':
/Users/jimlee/Documents/Arduino/libraries/LC_baseTools/textBuff.cpp:30:7: warning: variable 'oldestChar' set but not used [-Wunused-but-set-variable]
  char oldestChar;
       ^

I need to pull out a char from a buffer. So, I setup my readChar() method as it would like. Giving it somewhere to toss the char. The char IS ignored. Either way works. But this way I get these warnings.

The real issue is that to an un-informed user, the warning seem to come out of the blue. This is because of the standard way the compiler works. It tosses every library file it can find into the soup. So you get warnings from libraries you haven't even added to your project.

-jim lee

jimLee:
Giving it somewhere to toss the char.

Why bother giving it "somewhere to toss the char"?

(void) readChar();

If you are just removing a byte from a buffer you do not need to store the unwanted data in a variable.

bool textBuff::addChar(char inChar) 
{	
     if (full() && overwrite)   // If we're full and overwrite is true..
     {
	readChar();	// Make room by dumping the oldest char.
      }
}

Should do the same.

It tosses every library file it can find into the soup.

Yeah, I kind of see that, too. Though most of the time the libraries are included by other libraries.

The compiler has gotten more strict over the years. When I do get a warning like that I always go to library manager (or git hub) and make sure that I have the latest version of the library. Obtaining the latest version often clears away the warnings.

groundFungus:
Though most of the time the libraries are included by other libraries.

A library will only be compiled if it was included somewhere in the code. However, you might get warnings from the arduino-builder about problems with the file structure or metadata (but not code) of libraries that weren’t compiled from arduino-builder. This is because it scans all installed library folders and metadata during dependency resolution, and sometimes (more so with previous IDE versions) prints warnings about issues it finds while doing so.

jimLee:
The compiler tosses up a warning about setting but not using "oldestChar".

I have recently started to watch compile warnings- I think it will make me a better programmer to fix things before the compile fixes them for me.

I am surprised now many libraries throw warnings.

In your case:

oldestChar = readChar(); // Make room by dumping the oldest char.
oldestChar = oldestChar;  //Fools the compiler into thinking I am using the variable.

Wastes a few bytes, but eliminates the warning.

Or you could simply make it obvious that you’re dumping, and not waste any bytes with (void)readChar(); as has already been suggested.

gfvalvo:
Why bother giving it "somewhere to toss the char"?

(void) readChar();

I like that (void) thing. At least it is saying its to be ignored.

-jim lee