Skip to content

Commit

Permalink
Fix 'local variable is never mutated' errors
Browse files Browse the repository at this point in the history
  • Loading branch information
squeek502 committed Nov 21, 2023
1 parent e29db80 commit b5970a8
Show file tree
Hide file tree
Showing 23 changed files with 67 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/bmp.zig
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub fn read(reader: anytype, max_size: u64) ReadError!BitmapInfo {
var dib_header_buf: [@sizeOf(BITMAPCOREHEADER)]u8 align(@alignOf(BITMAPCOREHEADER)) = undefined;
std.mem.writeInt(u32, dib_header_buf[0..4], bitmap_info.dib_header_size, .little);
reader.readNoEof(dib_header_buf[4..]) catch return error.UnexpectedEOF;
var dib_header: *BITMAPCOREHEADER = @ptrCast(&dib_header_buf);
const dib_header: *BITMAPCOREHEADER = @ptrCast(&dib_header_buf);
structFieldsLittleToNative(BITMAPCOREHEADER, dib_header);

// > The size of the color palette is calculated from the BitsPerPixel value.
Expand Down
10 changes: 5 additions & 5 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,15 @@ pub const Options = struct {
// we shouldn't change anything.
if (val_ptr.* == .undefine) return;
// Otherwise, the new value takes precedence.
var duped_value = try self.allocator.dupe(u8, value);
const duped_value = try self.allocator.dupe(u8, value);
errdefer self.allocator.free(duped_value);
val_ptr.deinit(self.allocator);
val_ptr.* = .{ .define = duped_value };
return;
}
var duped_key = try self.allocator.dupe(u8, identifier);
const duped_key = try self.allocator.dupe(u8, identifier);
errdefer self.allocator.free(duped_key);
var duped_value = try self.allocator.dupe(u8, value);
const duped_value = try self.allocator.dupe(u8, value);
errdefer self.allocator.free(duped_value);
try self.symbols.put(self.allocator, duped_key, .{ .define = duped_value });
}
Expand All @@ -183,7 +183,7 @@ pub const Options = struct {
action.* = .{ .undefine = {} };
return;
}
var duped_key = try self.allocator.dupe(u8, identifier);
const duped_key = try self.allocator.dupe(u8, identifier);
errdefer self.allocator.free(duped_key);
try self.symbols.put(self.allocator, duped_key, .{ .undefine = {} });
}
Expand Down Expand Up @@ -828,7 +828,7 @@ pub fn parse(allocator: Allocator, args: []const []const u8, diagnostics: *Diagn
}
}

var positionals = args[arg_i..];
const positionals = args[arg_i..];

if (positionals.len < 1) {
var err_details = Diagnostics.ErrorDetails{ .print_args = false, .arg_index = arg_i };
Expand Down
6 changes: 3 additions & 3 deletions src/code_pages.zig
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ pub const Utf8 = struct {

pub fn decode(bytes: []const u8) Codepoint {
std.debug.assert(bytes.len > 0);
var first_byte = bytes[0];
var expected_len = sequenceLength(first_byte) orelse {
const first_byte = bytes[0];
const expected_len = sequenceLength(first_byte) orelse {
return .{ .value = Codepoint.invalid, .byte_len = 1 };
};
if (expected_len == 1) return .{ .value = first_byte, .byte_len = 1 };
Expand Down Expand Up @@ -367,7 +367,7 @@ pub const Utf8 = struct {

test "Utf8.WellFormedDecoder" {
const invalid_utf8 = "\xF0\x80";
var decoded = Utf8.WellFormedDecoder.decode(invalid_utf8);
const decoded = Utf8.WellFormedDecoder.decode(invalid_utf8);
try std.testing.expectEqual(Codepoint.invalid, decoded.value);
try std.testing.expectEqual(@as(usize, 2), decoded.byte_len);
}
Expand Down
8 changes: 4 additions & 4 deletions src/comments.zig
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ inline fn handleMultilineCarriageReturn(
}

pub fn removeCommentsAlloc(allocator: Allocator, source: []const u8, source_mappings: ?*SourceMappings) ![]u8 {
var buf = try allocator.alloc(u8, source.len);
const buf = try allocator.alloc(u8, source.len);
errdefer allocator.free(buf);
var result = try removeComments(source, buf, source_mappings);
const result = try removeComments(source, buf, source_mappings);
return allocator.realloc(buf, result.len);
}

Expand Down Expand Up @@ -344,7 +344,7 @@ test "remove comments with mappings" {
try mappings.set(3, 3, 0);
defer mappings.deinit(allocator);

var result = try removeComments(&mut_source, &mut_source, &mappings);
const result = try removeComments(&mut_source, &mut_source, &mappings);

try std.testing.expectEqualStrings("blahblah", result);
try std.testing.expectEqual(@as(usize, 1), mappings.end_line);
Expand All @@ -353,6 +353,6 @@ test "remove comments with mappings" {

test "in place" {
var mut_source = "blah /* comment */ blah".*;
var result = try removeComments(&mut_source, &mut_source, null);
const result = try removeComments(&mut_source, &mut_source, null);
try std.testing.expectEqualStrings("blah blah", result);
}
8 changes: 4 additions & 4 deletions src/compile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ pub const Compiler = struct {
},
},
.dib => {
var bitmap_header: *ico.BitmapHeader = @ptrCast(@alignCast(&header_bytes));
const bitmap_header: *ico.BitmapHeader = @ptrCast(@alignCast(&header_bytes));
if (native_endian == .big) {
std.mem.byteSwapAllFields(ico.BitmapHeader, bitmap_header);
}
Expand Down Expand Up @@ -1843,13 +1843,13 @@ pub const Compiler = struct {
}
try data_writer.writeByteNTimes(0, num_padding);

var style = if (control.style) |style_expression|
const style = if (control.style) |style_expression|
// Certain styles are implied by the control type
evaluateFlagsExpressionWithDefault(res.ControlClass.getImpliedStyle(control_type), style_expression, self.source, self.input_code_pages)
else
res.ControlClass.getImpliedStyle(control_type);

var exstyle = if (control.exstyle) |exstyle_expression|
const exstyle = if (control.exstyle) |exstyle_expression|
evaluateFlagsExpressionWithDefault(0, exstyle_expression, self.source, self.input_code_pages)
else
0;
Expand Down Expand Up @@ -3263,7 +3263,7 @@ pub const StringTable = struct {
const trimmed_string = trim: {
// Two NUL characters in a row act as a terminator
// Note: This is only the case for STRINGTABLE strings
var trimmed = trimToDoubleNUL(u16, utf16_string);
const trimmed = trimToDoubleNUL(u16, utf16_string);
// We also want to trim any trailing NUL characters
break :trim std.mem.trimRight(u16, trimmed, &[_]u16{0});
};
Expand Down
2 changes: 1 addition & 1 deletion src/lang.zig
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub fn tagToId(tag: []const u8) error{InvalidLanguageTag}!?LanguageId {
var normalized_buf: [longest_known_tag]u8 = undefined;
// To allow e.g. `de-de_phoneb` to get looked up as `de-de`, we need to
// omit the suffix, but only if the tag contains a valid alternate sort order.
var tag_to_normalize = if (parsed.isSuffixValidSortOrder()) tag[0 .. tag.len - (parsed.suffix.?.len + 1)] else tag;
const tag_to_normalize = if (parsed.isSuffixValidSortOrder()) tag[0 .. tag.len - (parsed.suffix.?.len + 1)] else tag;
const normalized_tag = normalizeTag(tag_to_normalize, &normalized_buf);
return std.meta.stringToEnum(LanguageId, normalized_tag) orelse {
// special case for a tag that has been mapped to the same ID
Expand Down
14 changes: 7 additions & 7 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn main() !void {
const stderr_config = std.io.tty.detectConfig(stderr);

var options = options: {
var args = try std.process.argsAlloc(allocator);
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);

var cli_diagnostics = cli.Diagnostics.init(allocator);
Expand Down Expand Up @@ -60,7 +60,7 @@ pub fn main() !void {
try stdout_writer.writeByte('\n');
}

var full_input = full_input: {
const full_input = full_input: {
if (options.preprocess != .no) {
var argv = std.ArrayList([]const u8).init(allocator);
defer argv.deinit();
Expand Down Expand Up @@ -118,7 +118,7 @@ pub fn main() !void {
try stdout_writer.print("{s}\n\n", .{argv.items[argv.items.len - 1]});
}

var result = std.ChildProcess.run(.{
const result = std.ChildProcess.run(.{
.allocator = allocator,
.argv = argv.items,
.max_output_bytes = std.math.maxInt(u32),
Expand Down Expand Up @@ -173,7 +173,7 @@ pub fn main() !void {
// may be a mismatch in how the line directive strings are parsed versus
// how they are escaped/written by the preprocessor.

var final_input = removeComments(mapping_results.result, mapping_results.result, &mapping_results.mappings) catch |err| switch (err) {
const final_input = removeComments(mapping_results.result, mapping_results.result, &mapping_results.mappings) catch |err| switch (err) {
error.InvalidSourceMappingCollapse => {
try renderErrorMessage(stderr.writer(), stderr_config, .err, "failed during comment removal; this is a known bug", .{});
std.os.exit(1);
Expand Down Expand Up @@ -302,7 +302,7 @@ const Preprocessor = enum {
try argv.appendSlice(pre.getCommandArgs());
try argv.append("--version");

var result = std.ChildProcess.run(.{
const result = std.ChildProcess.run(.{
.allocator = allocator,
.argv = argv.items,
.max_output_bytes = std.math.maxInt(u16),
Expand Down Expand Up @@ -385,7 +385,7 @@ const Preprocessor = enum {

fn zigSupportsLibcIncludesOption(allocator: std.mem.Allocator) !bool {
if (Preprocessor.zig_supports_libc_includes_option == null) {
var result = std.ChildProcess.run(.{
const result = std.ChildProcess.run(.{
.allocator = allocator,
.argv = &.{ "zig", "libc", "-includes" },
.max_output_bytes = std.math.maxInt(u16),
Expand All @@ -407,7 +407,7 @@ const Preprocessor = enum {
}

fn getIncludeDirsFromZig(allocator: std.mem.Allocator, target: []const u8) ![]const []const u8 {
var result = try std.ChildProcess.run(.{
const result = try std.ChildProcess.run(.{
.allocator = allocator,
.argv = &.{ "zig", "libc", "-includes", "-target", target },
.max_output_bytes = std.math.maxInt(u16),
Expand Down
12 changes: 6 additions & 6 deletions src/parse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub const Parser = struct {
// because it almost always leads to unhelpful error messages
// (usually it will end up with bogus things like 'file
// not found: {')
var statement = try self.parseStatement();
const statement = try self.parseStatement();
try statements.append(statement);
}
}
Expand Down Expand Up @@ -712,7 +712,7 @@ pub const Parser = struct {
.dlginclude => {
const common_resource_attributes = try self.parseCommonResourceAttributes();

var filename_expression = try self.parseExpression(.{
const filename_expression = try self.parseExpression(.{
.allowed_types = .{ .string = true },
});

Expand Down Expand Up @@ -770,7 +770,7 @@ pub const Parser = struct {
return &node.base;
}

var filename_expression = try self.parseExpression(.{
const filename_expression = try self.parseExpression(.{
// Don't tell the user that numbers are accepted since we error on
// number expressions and regular number literals are treated as unquoted
// literals rather than numbers, so from the users perspective
Expand Down Expand Up @@ -948,8 +948,8 @@ pub const Parser = struct {
style = try optional_param_parser.parse(.{ .not_expression_allowed = true });
}

var exstyle: ?*Node = try optional_param_parser.parse(.{ .not_expression_allowed = true });
var help_id: ?*Node = switch (resource) {
const exstyle: ?*Node = try optional_param_parser.parse(.{ .not_expression_allowed = true });
const help_id: ?*Node = switch (resource) {
.dialogex => try optional_param_parser.parse(.{}),
else => null,
};
Expand Down Expand Up @@ -1540,7 +1540,7 @@ pub const Parser = struct {

pub fn toErrorDetails(options: ParseExpressionOptions, token: Token) ErrorDetails {
// TODO: expected_types_override interaction with is_known_to_be_number_expression?
var expected_types = options.expected_types_override orelse ErrorDetails.ExpectedTypes{
const expected_types = options.expected_types_override orelse ErrorDetails.ExpectedTypes{
.number = options.allowed_types.number,
.number_expression = options.allowed_types.number,
.string_literal = options.allowed_types.string and !options.is_known_to_be_number_expression,
Expand Down
4 changes: 2 additions & 2 deletions src/res.zig
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ pub const NameOrOrdinal = union(enum) {
/// RC compiler would have allowed them, so that a proper warning/error
/// can be emitted.
pub fn maybeNonAsciiOrdinalFromString(bytes: SourceBytes) ?NameOrOrdinal {
var buf = bytes.slice;
const buf = bytes.slice;
const radix = 10;
if (buf.len > 2 and buf[0] == '0') {
switch (buf[1]) {
Expand Down Expand Up @@ -514,7 +514,7 @@ test "NameOrOrdinal" {
{
var expected = blk: {
// the input before the 𐐷 character, but uppercased
var expected_u8_bytes = "00614982008907933748980730280674788429543776231864944218790698304852300002973622122844631429099469274282385299397783838528QFFL7SHNSIETG0QKLR1UYPBTUV1PMFQRRA0VJDG354GQEDJMUPGPP1W1EXVNTZVEIZ6K3IPQM1AWGEYALMEODYVEZGOD3MFMGEY8FNR4JUETTB1PZDEWSNDRGZUA8SNXP3NGO";
const expected_u8_bytes = "00614982008907933748980730280674788429543776231864944218790698304852300002973622122844631429099469274282385299397783838528QFFL7SHNSIETG0QKLR1UYPBTUV1PMFQRRA0VJDG354GQEDJMUPGPP1W1EXVNTZVEIZ6K3IPQM1AWGEYALMEODYVEZGOD3MFMGEY8FNR4JUETTB1PZDEWSNDRGZUA8SNXP3NGO";
var buf: [256:0]u16 = undefined;
for (expected_u8_bytes, 0..) |byte, i| {
buf[i] = std.mem.nativeToLittle(u16, byte);
Expand Down
12 changes: 6 additions & 6 deletions src/source_mapping.zig
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ pub fn handleLineCommand(allocator: Allocator, line_command: []const u8, current
}

pub fn parseAndRemoveLineCommandsAlloc(allocator: Allocator, source: []const u8, options: ParseAndRemoveLineCommandsOptions) !ParseLineCommandsResult {
var buf = try allocator.alloc(u8, source.len);
const buf = try allocator.alloc(u8, source.len);
errdefer allocator.free(buf);
var result = try parseAndRemoveLineCommands(allocator, source, buf, options);
result.result = try allocator.realloc(buf, result.result.len);
Expand Down Expand Up @@ -487,15 +487,15 @@ pub const SourceMappings = struct {

/// Note: `line_num` and `corresponding_line_num` start at 1
pub fn set(self: *SourceMappings, line_num: usize, corresponding_line_num: usize, filename_offset: u32) !void {
var maybe_node = self.findNode(line_num);
const maybe_node = self.findNode(line_num);

var need_new_node = need_new_node: {
const need_new_node = need_new_node: {
if (maybe_node) |node| {
if (node.key.filename_offset != filename_offset) {
break :need_new_node true;
}
var exist_delta = @as(i64, @intCast(node.key.corresponding_start_line)) - @as(i64, @intCast(node.key.start_line));
var cur_delta = @as(i64, @intCast(corresponding_line_num)) - @as(i64, @intCast(line_num));
const exist_delta = @as(i64, @intCast(node.key.corresponding_start_line)) - @as(i64, @intCast(node.key.start_line));
const cur_delta = @as(i64, @intCast(corresponding_line_num)) - @as(i64, @intCast(line_num));
if (exist_delta != cur_delta) {
break :need_new_node true;
}
Expand Down Expand Up @@ -551,7 +551,7 @@ pub const SourceMappings = struct {
pub fn collapse(self: *SourceMappings, line_num: usize, num_following_lines_to_collapse: usize) !void {
std.debug.assert(num_following_lines_to_collapse > 0);
var node = self.findNode(line_num).?;
var span_diff = num_following_lines_to_collapse;
const span_diff = num_following_lines_to_collapse;
if (node.key.start_line != line_num) {
const offset = line_num - node.key.start_line;
const key = Source{
Expand Down
4 changes: 2 additions & 2 deletions test/fuzz_rc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ pub fn zigMain() !void {
const allocator = gpa.allocator();

const stdin = std.io.getStdIn();
var data = try stdin.readToEndAlloc(allocator, std.math.maxInt(usize));
const data = try stdin.readToEndAlloc(allocator, std.math.maxInt(usize));
defer allocator.free(data);

const dummy_filename = "fuzz.rc";

var mapping_results = try resinator.source_mapping.parseAndRemoveLineCommands(allocator, data, data, .{ .initial_filename = dummy_filename });
defer mapping_results.mappings.deinit(allocator);

var final_input = try resinator.comments.removeComments(mapping_results.result, mapping_results.result, &mapping_results.mappings);
const final_input = try resinator.comments.removeComments(mapping_results.result, mapping_results.result, &mapping_results.mappings);

var diagnostics = resinator.errors.Diagnostics.init(allocator);
defer diagnostics.deinit();
Expand Down
2 changes: 1 addition & 1 deletion test/fuzz_winafl.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn main() !void {
}

{
var tmp_dir = try std.process.getEnvVarOwned(allocator, "TEMP");
const tmp_dir = try std.process.getEnvVarOwned(allocator, "TEMP");
defer allocator.free(tmp_dir);

std.mem.copy(u8, &tmp_buf, tmp_dir);
Expand Down
2 changes: 1 addition & 1 deletion test/fuzzy_ascii_strings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ test "single char escapes" {
test "fuzz" {
const allocator = std.testing.allocator;
var random = std.rand.DefaultPrng.init(0);
var rand = random.random();
const rand = random.random();

var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
Expand Down
4 changes: 2 additions & 2 deletions test/fuzzy_bitmaps.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test "BITMAP fuzz" {

const bmp_file_header_len = 14;
const dib_header_len = 40;
var real_data_size = random_bytes_len + bmp_file_header_len + dib_header_len;
const real_data_size = random_bytes_len + bmp_file_header_len + dib_header_len;

const reported_data_size = if (rand.boolean())
// essentially `real_data_size -| rand.int(i8)`
Expand Down Expand Up @@ -70,7 +70,7 @@ test "BITMAP fuzz" {
// and now a bunch of random bytes
try image_buffer.ensureUnusedCapacity(random_bytes_len);

var slice_to_fill = image_buffer.unusedCapacitySlice()[0..random_bytes_len];
const slice_to_fill = image_buffer.unusedCapacitySlice()[0..random_bytes_len];
rand.bytes(slice_to_fill);

image_buffer.items.len += random_bytes_len;
Expand Down
2 changes: 1 addition & 1 deletion test/fuzzy_code_pages.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ test "windows-1252 mappings" {
test "fuzz" {
const allocator = std.testing.allocator;
var random = std.rand.DefaultPrng.init(0);
var rand = random.random();
const rand = random.random();

var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
Expand Down
2 changes: 1 addition & 1 deletion test/fuzzy_fonts.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test "FONT fuzz" {
// Just a bunch of random bytes
const random_bytes_len = rand.uintLessThanBiased(u32, max_file_len);
try font_buffer.ensureUnusedCapacity(random_bytes_len);
var slice_to_fill = font_buffer.unusedCapacitySlice()[0..random_bytes_len];
const slice_to_fill = font_buffer.unusedCapacitySlice()[0..random_bytes_len];
rand.bytes(slice_to_fill);
font_buffer.items.len += random_bytes_len;

Expand Down
2 changes: 1 addition & 1 deletion test/fuzzy_icons.zig
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ test "ICON fuzz" {
// and now a bunch of random bytes
try icon_buffer.ensureUnusedCapacity(random_bytes_len);

var slice_to_fill = icon_buffer.unusedCapacitySlice()[0..random_bytes_len];
const slice_to_fill = icon_buffer.unusedCapacitySlice()[0..random_bytes_len];
rand.bytes(slice_to_fill);

icon_buffer.items.len += random_bytes_len;
Expand Down
2 changes: 1 addition & 1 deletion test/fuzzy_name_or_ordinal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const iterations = fuzzy_options.max_iterations;
test {
const allocator = std.testing.allocator;
var random = std.rand.DefaultPrng.init(0);
var rand = random.random();
const rand = random.random();

var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
Expand Down
Loading

0 comments on commit b5970a8

Please sign in to comment.