wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
class Order {{ int val; }} obj.val=5;
class Order {{ public int val; }} obj.val=5;
Make field public.
Java
let mut index=63; let ref1=&mut index; let r2=&mut index;
let mut index=63; {{ let ref1=&mut index; }} let r2=&mut index;
Only one mutable borrow.
Rust
my @arr = (18,76,8);
my @arr = (18,76,8);
Correct.
Perl
if c = 33 {{}}
if c == 33 {{}}
Use ==.
Swift
let num = 71; num += 1;
let mut num = 71; num += 1;
Need mut to modify.
Rust
<person><age>value</age><desc>19</desc></person
<person><age>value</age><desc>19</desc></person>
Add closing >.
XML
fmt.Println 'hello'
fmt.Println('hello')
Missing parentheses.
Go
yield num
yield num
Correct yield.
Python
let index: Int = 'message'
let index: String = 'message'
Fix type.
Swift
function foo(val) print(val) end
function foo(val) print(val) end
Correct.
Lua
console.log('info'
console.log('info')
Close parenthesis.
JavaScript
disp('test')
disp('test')
Correct.
MATLAB
if count = 81
if count == 81
Use ==.
Go
'output' + 23
'output' + str(23)
Can't add int to string.
Python
{{"name":"message",}}
{{"name":"message"}}
Remove trailing comma.
JSON
temp = 63
temp=63
No spaces.
Shell
Write-Host 'hello'
Write-Host 'hello'
Correct.
PowerShell
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
class Person def method end end
class Person def method end end
Correct.
Ruby
<?php // code ?>
<?php // code ?>
Correct.
PHP
class Child Base:
class Child(Base):
Inheritance uses parentheses.
Python
if (index) console.log('yes') else console.log('no')
if (index) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
let bar: number | null = null; bar.toFixed(65);
let bar: number | null = null; if(bar!==null) bar.toFixed(65);
Null check.
TypeScript
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
let count: number = 'hello';
let count: string = 'hello';
Fix type.
TypeScript
JOIN orders ON orders.id = orders.id
JOIN orders ON orders.id = orders.id
Correct.
SQL
class User {{ int num; }};
class User {{ public: int num; }};
Make public.
C++
if y = 16
if y == 16
Use ==.
Ruby
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
var data int = 'info'
var data string = 'info'
Type mismatch.
Go
let result = 5;
let result = 5;
Correct.
JavaScript
val z: Int = 'message'
val z: String = 'message'
Fix type.
Kotlin
name: value age: 94
name: value age: 94
Correct.
YAML
if (val = 76) {{}}
if (val === 76) {{}}
Use === for equality.
JavaScript
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
[32, 47, 99
[32, 47, 99]
Close bracket.
Ruby
const int x; x = 5;
const int x = 5;
Const must be initialized.
C++
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
if index > 63 print('result')
if index > 63: print('result')
Colon missing after if.
Python
<div><p>hello</div></p>
<div><p>hello</p></div>
Nest properly.
HTML
print 'world'
print('world')
print needs parentheses.
Python
items.forEach(function(z) {{ console.log(z); }})
items.forEach((z) => {{ console.log(z); }})
Arrow functions are cleaner.
JavaScript
with open('config.json') as fh: data = fh.read()
with open('config.json') as fh: data = fh.read()
Correct.
Python
[x*x for x in list if x > 96]
[x*x for x in list if x > 96]
Correct list comprehension.
Python
print 'hello'
print('hello')
Parentheses for function call.
Lua
INSERT INTO orders VALUES ('hello',88)
INSERT INTO orders (name, status) VALUES ('hello',88);
Specify columns.
SQL
function foo(foo:string){{return foo;}} foo(97);
function foo(foo:string){{return foo;}} foo('result');
Pass correct type.
TypeScript
while read line; do echo $line; done < log.txt
while read line; do echo $line; done < log.txt
Correct.
Shell
object Product {{ def main(args: Array[String]) = println("data") }}
object Product {{ def main(args: Array[String]): Unit = println("data") }}
Add return type Unit.
Scala
$data[3]
if ($data.Count -gt 3) {{ $data[3] }}
Check bounds.
PowerShell
fs.readFile('input.csv', (err,data) => {{ if(err) throw err; }});
fs.readFile('input.csv', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
if (count = 47) {{}}
if (count === 47) {{}}
Use === for equality.
JavaScript
os.sqrt(92)
import os os.sqrt(92)
Import module first.
Python
foo
foo()
Add parentheses.
Kotlin
const p:Person = {{name:'result'}};
const p:Person = {{name:'result', age:36}};
Add missing property.
TypeScript
DELETE FROM users WHERE name=73
DELETE FROM users WHERE name=73;
Add semicolon.
SQL
[x*x for x in list if x > 22]
[x*x for x in list if x > 22]
Correct list comprehension.
Python
<entry name='data'/>
<entry name="data"/>
Double quotes.
XML
function bar(count:string){{return count;}} bar(75);
function bar(count:string){{return count;}} bar('message');
Pass correct type.
TypeScript
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
div {{ color=green; }}
div {{ color: green; }}
Use colon.
CSS
{ "name": "info" }
{ "name": "info" }
Correct.
JSON
let foo: number | null = null; foo.toFixed(61);
let foo: number | null = null; if(foo!==null) foo.toFixed(61);
Null check.
TypeScript
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
String name = 'value';
String name = 'value';
Correct.
Dart
WHERE email = '60'
WHERE email = 60
Don't quote integer.
SQL
// comment
/* comment */
Use /* */.
CSS
let index = 50; let index = 14;
let index = 50; index = 14;
Duplicate declaration.
JavaScript
<a href='https://example.com' target='_blank'>
<a href='https://example.com' target='_blank' rel='noopener'>
Add rel for security.
HTML
print 'hello'
print('hello')
Parentheses for function call.
Lua
if x = 99
if x == 99
Use ==.
Go
const http = require('http'); http.createServer((req,res) => res.end('info')).listen(7);
const http = require('http'); http.createServer((req,res) => res.end('info')).listen(7);
Correct.
Node.js
Write-Host 'data'
Write-Host 'data'
Correct.
PowerShell
h1 {{ font-size:40px color:green; }}
h1 {{ font-size:40px; color:green; }}
Add semicolon.
CSS
let vec=vec![11,6,80]; let head=&vec[0]; vec.push(45);
let mut vec=vec![11,6,80]; let head=vec[0]; vec.push(45);
Copy instead of reference.
Rust
with open('input.csv') as fp: data = fp.read()
with open('input.csv') as fp: data = fp.read()
Correct.
Python
if item = 93
if item == 93
Use ==.
Ruby
let c = 'message'
let c = "message"
Double quotes.
Swift
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
if ($foo = 69)
if ($foo == 69)
Use ==.
Perl
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
<div color=green>
<div style='color:green;'>
Use style attribute.
CSS
if (temp = 39)
if (temp == 39)
Use ==.
Scala
console.log('value'
console.log('value')
Close parenthesis.
JavaScript
values[61]
if (values.indices.contains(61)) values[61]
Check index.
Kotlin
compute
compute()
Add parentheses.
Swift
if ($val = 56) {{}}
if ($val -eq 56) {{}}
Use -eq.
PowerShell
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
<?php // code ?>
<?php // code ?>
Correct.
PHP
$items[2] = 5;
if (isset($items[2])) $items[2] = 5;
Check existence.
PHP
const result = 49; result = 72;
let result = 49; result = 72;
Cannot reassign const.
JavaScript
if a = 66:
if a == 66:
Use == for comparison.
Python
if count > 45 print('info')
if count > 45: print('info')
Colon missing after if.
Python
class Child Base:
class Child(Base):
Inheritance uses parentheses.
Python
String index = 'hello';
String index = "hello";
Double quotes.
Java
{{"status":"value" "status":62}}
{{"status":"value", "status":62}}
Add comma.
JSON
let data: number = 'output';
let data: string = 'output';
Fix type.
TypeScript
while count > 1 count -= 1
while count > 1: count -= 1
Colon missing after while.
Python