Unused Code Warnings

I've got an executable crate that uses OpenGL to render images to the screen as textures. I've got a few boilerplate modules that abstract a lot of the OpenGL setup, and also wraps the unsafe OpenGL calls in safe code making OpenGL an implementation detail to the rest of the app. That all works fine, but I'm getting 30 warnings about unused methods and enum variants because I haven't needed to directly use some of that code yet. I will eventually need it, but not now.

The warnings make total sense, but I hate seeing them show up in my console every time I go to build something. I could allow the dead code, but that feels a bit messy as I'd have to declare the allow() macro everywhere. Plus, once I finally use that code, I'll forget about it.

What kind of mindset should I have around this? Would it be best to allow the dead code? I'll eventually break all of this code out into its own crate since this will be useful across multiple crates. I only hesitate on abstracting my little graphics toolkit because I'm not completely sure how I'll want to setup my crates, workspaces, and repos yet.

You can either put an #![allow(dead_code)] on the module, or make them sufficiently public that the error goes away.

Ok, it sounds like #[allow(dead_code)] might be the way I'll have to go.

If I went the public route, does that only work if the crate is an executable? I've got this enum that's public along with the methods attached to it, and I'm getting unused code warnings for all variants except for the one I'm currently using. Here's what my enum looks like:

pub enum AttributeKind {
	Byte,
	Short,
	Int,
	UnsignedByte,
	UnsignedShort,
	UnsignedInt,
	Half,
	Float,
	Double,
	Fixed,
}

impl AttributeKind {
	pub fn to_raw_enum(&self) -> GLenum {
		match self {
			AttributeKind::Byte => gl::BYTE,
			AttributeKind::Short => gl::SHORT,
			AttributeKind::Int => gl::INT,
			AttributeKind::UnsignedByte => gl::UNSIGNED_BYTE,
			AttributeKind::UnsignedShort => gl::UNSIGNED_SHORT,
			AttributeKind::UnsignedInt => gl::UNSIGNED_INT,
			AttributeKind::Half => gl::HALF_FLOAT,
			AttributeKind::Float => gl::FLOAT,
			AttributeKind::Double => gl::DOUBLE,
			AttributeKind::Fixed => gl::FIXED,
		}
	}

	pub fn size(&self) -> usize {
		match self {
			AttributeKind::Byte => mem::size_of::<GLchar>(),
			AttributeKind::Short => mem::size_of::<GLshort>(),
			AttributeKind::Int => mem::size_of::<GLint>(),
			AttributeKind::UnsignedByte => mem::size_of::<GLbyte>(),
			AttributeKind::UnsignedShort => mem::size_of::<GLushort>(),
			AttributeKind::UnsignedInt => mem::size_of::<GLuint>(),
			AttributeKind::Half => mem::size_of::<GLhalf>(),
			AttributeKind::Float => mem::size_of::<GLfloat>(),
			AttributeKind::Double => mem::size_of::<GLdouble>(),
			AttributeKind::Fixed => mem::size_of::<GLfixed>(),
		}
	}
}

I think that making it public will only silence the warning if your crate is a library, and the item is accessible from outside the crate. Making things pub won't be enough, for instance, if the whole module they're in is private so there's no way to address them from outside the crate.

One solution could then be to put all the boilerplate you expect to reuse in a separate crate, and make all the functionality you want to expose an actual public interface to the crate. Then it could still display actual unused code warnings if there was any code accidentally not included in the public interface.

That makes sense. I will make that a separate crate then to silence the errors. Those modules were meant to be pretty reusable since there's been a lot of need for all of them.

1 Like

The reason each of those enum variants are being flagged as warnings is because there are no specific use-case of each of those variants being explicitly used in this crate. This code is essentially an idiomatic, safe interface around the lower-level, unsafe gl crate. This was meant to cover all OpenGL's use-cases, and not necessarily any specific use-cases of the crate that will be using this.

Given the separation of concerns, it seems logical for this crate to provide an enum with these variants, and provide functions that take this enum as an input parameter, but not specifically instance and use every variant within this crate. I think that's why these warnings are silenced when they're exported part of the library's API: because they're meant to be used by outside crates that use this crate as a dependency.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.